create-subclass.js

create-subclass.js

This is a function to implement inheritance using javascript, which is more concise and elegant than the commonly used inheritance.
这是一个使用javascript实现继承的方法,这个方法比常用的继承写法更精简和优雅(呵呵,我认为)。


repository

github

码云

Use

 var Subclass = SuperClass.create({
     //attributes
 }, {
     //prototype object attributes
 });

or

 var Subclass = SuperClass.create(function(/*arguments*/) {
     //get arguments
    return {
        //attributes
    }
 }, {
     //prototype object attributes
 });

demo

Element
function Element(ctx, style) {
    this.ctx = ctx;
    this.style = style;
}

Element.prototype = {
    paint: function () {
    },
    render: function () {
        this.paint();
    }
};
Rect inherit the Element
var Rect = Element.create({
    type: 'rect'
}, {
    shape: function () {
        var s = this.style;
        this.ctx.beginPath();
        this.ctx.fillStyle = s.color;
        this.ctx.rect(s.x, s.y, s.w, s.h);
        this.ctx.fill();
        this.ctx.closePath();
    },
    paint: function () {
        this.shape();
    }
});
TextRect inherit the Rect
var TextRect = Rect.create(function (ctx, style, textStyle) {
    return {
        textStyle: textStyle
    }
}, {
    text: function () {
        var s = this.style;
        var ts = this.textStyle;
        this.ctx.beginPath();
        this.ctx.fillStyle = ts.color;
        this.ctx.font = ts.font;
        this.ctx.textAlign = ts.textAlign;
        this.ctx.textBaseline = ts.textBaseline;
        this.ctx.fillText(ts.text, s.x + ts.x, s.y + ts.y);
        this.ctx.closePath();
    },
    paint: function () {
        this.shape();
        this.text();
    }
});

The equivalent is the following code

Element
function Element(ctx, style) {
    this.ctx = ctx;
    this.style = style;
}

Element.prototype = {
    paint: function () {
    },
    render: function () {
        this.paint();
    }
};
Rect inherit the Element
function Rect() {
    Element.apply(this, arguments);
    this.type = 'rect';
}

Rect.prototype = Object.create(Element.prototype);
Rect.prototype.constructor = Rect;
Rect.prototype.super = Element;
Rect.prototype.shape = function () {
    var s = this.style;
    this.ctx.beginPath();
    this.ctx.fillStyle = s.color;
    this.ctx.rect(s.x, s.y, s.w, s.h);
    this.ctx.fill();
    this.ctx.closePath();
};
Rect.prototype.paint = function () {
    this.shape();
};
TextRect inherit the Rect
function TextRect(ctx, style, textStyle) {
    Rect.apply(this, arguments);
    this.textStyle = textStyle;
}

TextRect.prototype = Object.create(Rect.prototype);
TextRect.prototype.constructor = TextRect;
TextRect.prototype.super = Rect;
TextRect.prototype.text = function () {
    var s = this.style;
    var ts = this.textStyle;
    this.ctx.beginPath();
    this.ctx.fillStyle = ts.color;
    this.ctx.font = ts.font;
    this.ctx.textAlign = ts.textAlign;
    this.ctx.textBaseline = ts.textBaseline;
    this.ctx.fillText(ts.text, s.x + ts.x, s.y + ts.y);
    this.ctx.closePath();
};
TextRect.prototype.paint = function () {
    this.shape();
    this.text();
};

License

MIT © porky-prince

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值