复制代码
(function(){
    function Extend(func,proto){
        func.prototype.__proto__=proto.prototype;
        Object.defineProperty(func.prototype,"proto",{
            value: proto.prototype
        });
    }
    function Super(func,method){
        if(!method) method='constructor';
        return func.prototype.__proto__[method];
    }
    window.Extend=Extend;
    window.Super=Super;
})();
复制代码

在处理super的super时候遇到了死循环:

this.super-->this.proto.constructor(){this.super}-->this.proto.constructor。。。

后来直接用了上面代码中的办法,不想整得太复杂(就是不会。。。)

复制代码
(function(){
    function AAA(name){
        this.name=name;
    }
    function BBB(name){
        Super(BBB).call(this,name);
    }
    Extend(BBB,AAA);
    function CCC(name,age){
        Super(CCC).call(this,name);
        this.age=age;
    }
    Extend(CCC,BBB);
    var c=new CCC('ccc',18);
    console.log(c);
})();
复制代码

然后不想污染Function,只能污染window了。。。

话说放在Function里面是不是要好用些?

复制代码
(function(){
    Function.prototype.Extend=function(proto){
        this.prototype.__proto__=proto.prototype;
    }
    Function.prototype.Super=function(method){
        if(!method) method='constructor';
        return this.prototype.__proto__[method];
    }
})();
(function(){
    function AAA(name){
        this.name=name;
    }
    function BBB(name){
        BBB.Super().call(this,name);
    }
    BBB.Extend(AAA);
    function CCC(name,age){
        CCC.Super().call(this,name);
        this.age=age;
    }
    CCC.Extend(BBB);
    var c=new CCC('ccc',18);
    console.log(c);
})();
复制代码
posted on 2016-11-29 13:16  王庆东mas  阅读(221)  评论(0编辑  收藏  举报