lufy's legend

标题: base()方法的一个问题 [打印本页]

作者: GreatToad    时间: 2014-2-7 21:50
标题: base()方法的一个问题
base()方法的一个问题,不知道算不算bug.
代码:

    function ClassA(){}
    ClassA.prototype.qqqq=undefined;

    function ClassB(){
        base(this,ClassA);
    }

    var classB=new ClassB(10);


报错: Uncaught TypeError: Cannot set property 'super' of undefined

function base(d,b,a){
        var p=null,o=d.constructor.prototype,h={};
        if(d.constructor.name == "Object"){
                console.warn( "When you use the extends. You must make a method like 'XX.prototype.xxx=function(){}'. but not 'XX.prototype={xxx:function(){}}'.");
        }
        for(p in o)h[p]=1;
        for(p in b.prototype){
                if(!h[p])o[p] = b.prototype[p];
                o[p][SUPER] = b.prototype;//应该是说o[p] 为 undefined, 即o["qqqq"] 为 undefined 的原因
        }
        b.apply(d,a);
}

因为其他框架的代码的继承方式有这种情况

//----其他框架
function Class1(){
    this.a=undefined;
}
function Class2(){}
Class2.prototype=new Class1();
//----其他框架

//自己的代码用base继承其他框架的Class2
function MyClass3(){
    base(this,Class2);
}

var myClass3=new MyClass3();



作者: lufy    时间: 2014-2-8 20:02
多谢指出,我下次再调整一下,争取做到兼容这种情况。




欢迎光临 lufy's legend (http://lufylegend.com/forum/) Powered by Discuz! X2.5