首页 > 数据库技术 > 详细

[DBW]一个小巧的Class方案

时间:2016-11-29 14:51:42      阅读:195      评论:0      收藏:0      [点我收藏+]
技术分享
(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);
})();
技术分享

[DBW]一个小巧的Class方案

原文:http://www.cnblogs.com/x-poior/p/6113193.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!