ES6的class语法 不是一种新的“类”机制,class基本上只是现有 [[prototype]]链(委托)机制的语法糖。
内容
代码如下:
// widget:部件,button:按钮
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 = $( "" ).text( this.label );
}
render($where) {
super.render( $where );<