lufy's legend

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 9183|回复: 1
打印 上一主题 下一主题

base()方法的一个问题

[复制链接]

4

主题

0

好友

32

积分

士兵

Rank: 1

跳转到指定楼层
楼主
发表于 2014-2-7 21:50:20 |只看该作者 |倒序浏览
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();


回复

使用道具 举报

37

主题

8

好友

9312

积分

诸侯王

Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15

沙发
发表于 2014-2-8 20:02:03 |只看该作者
多谢指出,我下次再调整一下,争取做到兼容这种情况。
不回答与技术和引擎不相关的问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

防止垃圾广告,请填写任意字符

Archiver|lufy's legend

GMT+8, 2024-5-5 19:49 , Processed in 0.049810 second(s), 20 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部