javascript中的继承,“父级”中的变量
我是第一次做 OO javascript。我读过有关继承和原型的内容,并认为我已经破解了它。直到我发现了这个小例子。
function TestObject(data)
{
this.test_array = [];
this.clone_array = [];
this.dosomestuff = function()
{
for(var i=0; i<this.test_array.length; i++)
{
this.clone_array[i]=this.test_array[i];
}
}
this.__construct = function(data)
{
console.log("Testing Object Values" ,this.clone_array);
this.test_array = data;
};
}
TestObject2.prototype = new TestObject();
function TestObject2(data)
{
this.__construct(data);
this.dothings = function()
{
this.dosomestuff();
}
}
如果我执行以下操作:
var foo = new TestObject2([1,2,3,4]);
foo.dothings();
var bar = new TestObject2([4,5,6]);
bar.dothings();
我希望控制台显示:
Testing Object Values, []
Testing Object Values, []
但是它显示:
Testing Object Values, []
Testing Object Values, [1,2,3,4]
问题当然是这个调用:
TestObject2.prototype = new TestObject();
如何让 TestObject 中的父变量“重置”,而不是在 __construct 方法中手动重置它们?
TestObject2 是否有另一种方法可以继承 TestObject 的所有值/方法,并使“new”的行为符合我所期望的 PHP OO 方式? (我确信 JS 这样做的方式真的很奇怪,就好像我的大脑在大学中正确地为我服务一样 Java 在这方面就像 PHP 一样)
I am doing OO javascript for the first time. I have read about inheritance and prototype and thought I had cracked it. Until I discovered this little example.
function TestObject(data)
{
this.test_array = [];
this.clone_array = [];
this.dosomestuff = function()
{
for(var i=0; i<this.test_array.length; i++)
{
this.clone_array[i]=this.test_array[i];
}
}
this.__construct = function(data)
{
console.log("Testing Object Values" ,this.clone_array);
this.test_array = data;
};
}
TestObject2.prototype = new TestObject();
function TestObject2(data)
{
this.__construct(data);
this.dothings = function()
{
this.dosomestuff();
}
}
If I do the following:
var foo = new TestObject2([1,2,3,4]);
foo.dothings();
var bar = new TestObject2([4,5,6]);
bar.dothings();
I would expect the console to show:
Testing Object Values, []
Testing Object Values, []
However it shows:
Testing Object Values, []
Testing Object Values, [1,2,3,4]
The problem is of course this call:
TestObject2.prototype = new TestObject();
How do I get the parent variables in TestObject to "reset" other than manually resetting them in the __construct method?
Is there another way of TestObject2 of inheriting all the values/methods from TestObject and for "new" to behave as I would expect in a PHP OO manner? (I am sure the way JS is doing this is really really odd as if my brain serves me correctly from University Java works like PHP in this regard)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上
将 TestObject 的单个实例放置在 TestObject2 的原型链中。因此,TestObject2 的任何实例都将修改 TestObject 中相同的单个实例后代属性。如果您将另一个 console.log 放入 TestObject 的构造函数中,您会发现它只被调用一次!
您可以在此处找到有关您的确切问题的更多详细信息。
您需要从 TextObject2 的构造函数中调用 TextObject 的构造函数,如下所示:
通过调用 TestObject 构造函数并在新 TestObject2 对象的范围内执行它,它会在 TestObject2 对象中创建 TestObject 的所有元素。
Basically
places a single instance of TestObject in the prototype chain of TestObject2. Thus, any instances of TestObject2 will modify the same single instance offspring property in TestObject. If you put another console.log in the construtor of TestObject you'll notice it only getting called once!
More details to your exact problem can be found here.
You need to call the constructor of TextObject from within the constructor of TextObject2 like this:
By calling the TestObject constructor function and executing it in the scope of (this is) the new TestObject2 object, it creates all elements of TestObject in TestObject2 object.
您是否已尝试将
this.clone_array = [];
定义到 TestObject2 中?have you already tried to define
this.clone_array = [];
into TestObject2 instead?我认为
是重写 TestObject2 的构造函数。
尝试
I think
is overriding the constructor of TestObject2.
Try