admin管理员组文章数量:1435859
I'm new to prototypal inheritance so I'm trying to understand the 'right' way. I thought I could do this:
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");
test.shout("test3", "hope");
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");
test.shout("test3", "hope");
But instead I get "tbase.BicData.prototype is undefined"
In Java speak, what I want is to have Tdata as a boilerplate 'interface', BicData to be an implementation of that that, and then to instantiate objects from it.
Where am I going wrong?
I'm new to prototypal inheritance so I'm trying to understand the 'right' way. I thought I could do this:
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");
test.shout("test3", "hope");
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");
test.shout("test3", "hope");
But instead I get "tbase.BicData.prototype is undefined"
In Java speak, what I want is to have Tdata as a boilerplate 'interface', BicData to be an implementation of that that, and then to instantiate objects from it.
Where am I going wrong?
Share Improve this question edited Feb 20, 2015 at 2:11 Kevin Brown-Silva 41.7k42 gold badges206 silver badges243 bronze badges asked Feb 19, 2010 at 16:56 robinhowlettrobinhowlett 1739 bronze badges1 Answer
Reset to default 8The problem is that tbase.BicData
is an object (tbase.BicData = Object.create(tData);
), and the prototype
property should be used on constructor functions.
Taking advantage of the Object.create
method, I would do something like this:
var tbase = {};
tbase.Tdata = {
say : function (data) {
console.log(data);
}
};
tbase.BicData = Object.create(tbase.Tdata);
tbase.BicData.say = function (data) {
console.log("overridden: " + data)
};
tbase.BicData.shout = function (data, temp) {
console.log("SHOUT: " + data + ", " + temp)
};
var test = Object.create(tbase.BicData);
var tData = Object.create(tbase.Tdata);
tData.say("test1"); // test1
test.say("test2"); // overridden: test2
test.shout("test3", "hope"); // SHOUT: test3, hope
本文标签: javascriptPrototypal inheritance Can you chain ObjectcreateStack Overflow
版权声明:本文标题:javascript - Prototypal inheritance: Can you chain Object.create? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745672481a2669653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论