js 继承 实例

var util=(function () {
    return{
        extend:function (subClass,supClass) {
            function F() {}
            F.prototype=supClass.prototype;
            subClass.prototype=new F();
            subClass.prototype.constructor=subClass;
            subClass.superClass=supClass;
            if(subClass.prototype.constructor===Object.prototype.constructor){
                supClass.prototype.constructor=supClass
            }
        },
        appendPrototype:function (target,source) {
            var names=Object.getOwnPropertyNames(source);
            for(var i=0;i<names.length;i++){
                var proto=Object.getOwnPropertyDescriptor(source,names[i]);
                Object.defineProperty(target,names[i],proto)
            }
        }
    }
})();
(function (win) {
    function DivElem(bg) {
        this.div=document.createElement("div");
        util.appendPrototype(this.div,DivElem.prototype);
        this.setBgColor(bg);
        return this.div;
    }
    DivElem.prototype={
        _width:0,
        _height:0,
        set width(value){
            this._width=value;
            this.style.width=value+"px";
        },
        get width(){
            return this._width;
        },
        set height(value){
            this._height=value;
            this.style.height=value+"px";
        },
        get height(){
            return this._height;
        },
        setBgColor:function (bg) {
            this.div.style.backgroundColor=bg;
            this.div.style.transform="rotate(60deg)";
        }
    };
    function BgDivElem(bg) {
        DivElem.call(this,bg);
        return this.div
    }
    util.extend(BgDivElem,DivElem);
    BgDivElem.prototype.setBgColor=function (bg) {
        BgDivElem.superClass.prototype.setBgColor.call(this,bg);
        this.div.style.border="5px solid #000000"
    };


    win.BgDivElem=BgDivElem;
    win.DivElem=DivElem;
})(window);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值