美文网首页
JS类风格,class,对象关联风格委托代码区别

JS类风格,class,对象关联风格委托代码区别

作者: VinSmokeW | 来源:发表于2021-01-29 12:22 被阅读0次

习惯了面向对象设计模式,所以很快会想到一个包含所有通用控件行为的父类
(可能叫作 Widget )和继承父类的特殊控件子类(比如 Button )。

1.纯JavaScript 实现类风格

下面这段代码展示的是如何在不使用任何“类”辅助库或者语法的情况下,使用纯
JavaScript 实现类风格的代码:

// 父类
function Widget(width,height) {
  this.width = width || 50;
  this.height = height || 50;
  this.$elem = null;
}
Widget.prototype.render = function($where){
  if (this.$elem) {
    this.$elem.css( {
      width: this.width + "px",
      height: this.height + "px"
    } ).appendTo( $where );
  }
};
// 子类
function Button(width,height,label) {
  // 调用“super”构造函数
  Widget.call( this, width, height );
  this.label = label || "Default";
  this.$elem = $( "<button>" ).text( this.label );
}
// 让 Button“继承”Widget
Button.prototype = Object.create( Widget.prototype );
// 重写 render(..)
Button.prototype.render = function($where) {
  // “super”调用
  Widget.prototype.render.call( this, $where );
  this.$elem.click( this.onClick.bind( this ) );
};
Button.prototype.onClick = function(evt) {
    console.log( "Button '" + this.label + "' clicked!" );
};
$( document ).ready( function(){
  var $body = $( document.body );
  var btn1 = new Button( 125, 30, "Hello" );
  var btn2 = new Button( 150, 40, "World" );
  btn1.render( $body );
  btn2.render( $body );
} );

2.ES6的 class 语法糖

这里简单介绍一下如何使用 class 来实现相同的功能:

class Widget {
  constructor(width,height) {
    this.width = width || 50;
    this.height = height || 50;
    
    this.$elem = null;
  }
  render($where){
    if (this.$elem) {
      this.$elem.css( {
        width: this.width + "px",
        height: this.height + "px"
      } ).appendTo( $where );
    }
  }
}
class Button extends Widget {
  constructor(width,height,label) {
    super( width, height );
    this.label = label || "Default";
    
    this.$elem = $( "<button>" ).text( this.label );
  }
  render($where) {
    super( $where );
    
    this.$elem.click( this.onClick.bind( this ) );
  }
  onClick(evt) {
    console.log( "Button '" + this.label + "' clicked!" );
  }
}
$( document ).ready( function(){
  var $body = $( document.body );
  
  var btn1 = new Button( 125, 30, "Hello" );
  var btn2 = new Button( 150, 40, "World" );
  
  btn1.render( $body );
  btn2.render( $body );
} );

3.委托控件对象

下面的例子使用对象关联风格委托来更简单地实现 Widget / Button :

var Widget = {
  init: function(width,height){
    this.width = width || 50;
    this.height = height || 50;
    this.$elem = null;
  },
  insert: function($where){
    if (this.$elem) {
      this.$elem.css( {
        width: this.width + "px",
        height: this.height + "px"
      } ).appendTo( $where );
    }
  }
};
var Button = Object.create( Widget );
Button.setup = function(width,height,label){
  // 委托调用
  this.init( width, height );
  this.label = label || "Default";
  
  this.$elem = $( "<button>" ).text( this.label );
};
Button.build = function($where) {
  // 委托调用
  this.insert( $where );
  
  this.$elem.click( this.onClick.bind( this ) );
};
Button.onClick = function(evt) {
    console.log( "Button '" + this.label + "' clicked!" );
};
$( document ).ready( function(){
  var $body = $( document.body );
  
  var btn1 = Object.create( Button );
  btn1.setup( 125, 30, "Hello" );
  var btn2 = Object.create( Button );
  btn2.setup( 150, 40, "World" );
  
  btn1.build( $body );
  btn2.build( $body );
} );

相关文章

  • JS类风格,class,对象关联风格委托代码区别

    习惯了面向对象设计模式,所以很快会想到一个包含所有通用控件行为的父类(可能叫作 Widget )和继承父类的特殊控...

  • 你不知道的JavaScript笔记之行为委托

    委托理论 相比于面向类(或者说面向对象),这种风格可以被称之为“对象关联”(OLOO,object linked ...

  • 图表_柱状图_动画效果

    js对象规划 js代码太多,需要复用,相当于类Class。如:内容组织类/图文组件类(基本类)/图表组件类 效果 ...

  • 6 行为委托

    JS中的原型链机制的本质就是对象之间的关联关系。 面向委托的设计 我们需要把思路从类和继承的设计模式转换到委托行为...

  • 面向对象(三)--编程

    面向对象,面向过程编程,函数式编程 面向过程和面向对象最基本的区别就是,代码的组织方式不同。 面向过程风格的代码被...

  • 前端规范-命名风格

    代码命名风格: js使用严格模式:'use strict' 1、ClassName 类的命名,首字母必须大写,大驼...

  • 代码风格(5)——类

    一、类应该短小 类和函数一样应该短小。对于函数,我们通过计算代码行数衡量大小。对于类,我们采用不同的衡量方法,计算...

  • 反射之Class类

    Class类 Class类常用方法 通过代码简单解释 运行结果 那些类型有Class对象

  • 基于对象和面向对象风格

    基于对象和面向对象风格这种叫法来源于陈硕的< >一书。其具体含义如下: 基于对象风格:具体类加全局函数的设计风格。...

  • NO.4 理论一:当谈论面向对象的时候,我们到底在谈论什么?

    Q 什么是面向对象编程?A 面向对象编程是一种编程范式(编程风格),它以类和对象作为组织代码的基本单元,并将封装,...

网友评论

      本文标题:JS类风格,class,对象关联风格委托代码区别

      本文链接:https://www.haomeiwen.com/subject/elsqtltx.html