zoukankan      html  css  js  c++  java
  • [DBW]一个小巧的Class方案

    复制代码
    (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);
    })();
    复制代码
  • 相关阅读:
    高斯消元
    逻辑运算符之优先级&&and、or
    康托展开
    关于bootstrap的双层遮罩问题
    写好页面在内网内访问
    swiper插件的一些坑
    第一篇博客
    poj 3415 Common Substrings
    poj 1509 Glass Beads
    poj 3260 The Fewest Coins
  • 原文地址:https://www.cnblogs.com/x-poior/p/6113193.html
Copyright © 2011-2022 走看看