[英]javascript dynamic prototype
I want extend a new JS object while creation with other object passing a parameter. 我希望在创建时扩展一个新的JS对象,其他对象传递一个参数。 This code does not work, because I only can extend object without dynamic parameter. 这段代码不起作用,因为我只能在没有动态参数的情况下扩展对象。
otherObject = function(id1){
this.id = id1;
};
otherObject.prototype.test =function(){
alert(this.id);
};
testObject = function(id2) {
this.id=id2;
};
testObject.prototype = new otherObject("id2");/* id2 should be testObject this.id */
var a = new testObject("variable");
a.test();
Any suggestion? 有什么建议吗?
Apart from the obvious syntax error, the correct JavaScript way of inheritance is this: 除了明显的语法错误之外,正确的JavaScript继承方式是这样的:
// constructors are named uppercase by convention
function OtherObject(id1) {
this.id = id1;
};
OtherObject.prototype.test = function() {
alert(this.id);
};
function TestObject(id2) {
// call "super" constructor on this object:
OtherObject.call(this, id2);
};
// create a prototype object inheriting from the other one
TestObject.prototype = Object.create(OtherObject.prototype);
// if you want them to be equal (share all methods), you can simply use
TestObject.prototype = OtherObject.prototype;
var a = new TestObject("variable");
a.test(); // alerts "variable"
You will find lots of tutorials about this on the web. 您将在网上找到许多关于此的教程。
I do not understand exactly what you desire, but 我不明白你想要什么,但是
otherObject.prototype.test = function () {
alert(this.id);
};
would be correct. 会是对的。
And this 还有这个
testObject.prototype = new otherObject(id2); testObject.prototype = new otherObject(id2);
will not work unless id2 is set before. 除非之前设置了id2,否则将无法工作。
Try the following 请尝试以下方法
var OtherObject = function () {
}
OtherObject.prototype.test = function () {
alert (this.id);
}
var TestObject = function (id) {
this.id = id;
}
TestObject.prototype = new OtherObject ();
var a = new TestObject("variable");
a.test ();
Fixed your code 修复了你的代码
otherObject = function(id1){
this.id = id1;
};
otherObject.prototype.test =function(){
alert(this.id);
};
testObject = function(id2) {
this.id=id2;
};
testObject.prototype = new otherObject("id2");/* id2 should be testObject this.id */
var a = new testObject("variable");
a.test();
testObject = function(id2) {
otherObject.call(this, id2); // run the parent object constructor with id2 parameter
this.id=id2;
};
testObject.prototype = new otherObject(); // no id2 parameter here, it doesn't make sense
Note that while creating an instance of testObject
, the constructor of otherObject
is called twice - once to create the prototype and once to initialise the object. 请注意,在创建实例testObject
的构造otherObject
被称为两次-一次创建原型,一旦初始化的对象。
To prevent duplicate initialisation, we can halt the constructor immediately when we are only using it to create the prototype. 为了防止重复初始化,我们可以在我们仅使用它来创建原型时立即停止构造函数。
otherObject = function(id1){
if (typeof id1 == 'undefined') {
/* as there is no parameter, this must be the call used to create
* the prototype. No initialisation needed here, we'll just return.
*/
return;
}
this.id = id1;
};
PS Please use capital camelcase with objects. PS请使用资本camelcase与对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.