小编rob*_*ett的帖子

原型继承:你可以链接Object.create吗?

我是原型继承的新手,所以我试图理解'正确'的方式.我以为我能做到这一点:

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

var tbase = {};

tbase.Tdata = function Tdata() {};

tbase.Tdata.prototype.say = function (data) {
    console.log(data);
};

var tData = new tbase.Tdata();

tbase.BicData = Object.create(tData);

tbase.BicData.prototype.say = function (data) {
    console.log("overridden: " + data)
};

tbase.BicData.prototype.shout = function (data, temp) {
    console.log("SHOUT: " + data + ", " + temp)
};

var test = new tbase.BicData();

tData.say("test1"); 
test.say("test2"); …
Run Code Online (Sandbox Code Playgroud)

javascript inheritance prototype object-create

6
推荐指数
1
解决办法
1715
查看次数