javascript中的继承,“父”中的变量。 [英] Inheritance in javascript, variables in the "parent"

查看:98
本文介绍了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];
}
}

这个.__构造=函数(数据)
{
console.log(测试对象值,this.clone_array);
this.test_array = data;
};
}

TestObject2.prototype = new TestObject();

函数TestObject2(数据)
{
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();

我希望控制台显示:

 测试对象值,[] 
测试对象值,[]

然而它显示:

 测试对象值,[] 
测试对象值,[1 ,2,3,4]

问题当然是这个电话:

  TestObject2.prototype = new TestObject(); 

除了在__construct中手动重置它们之外,如何让TestObject中的父变量重置方法?



TestObject2是否有另一种方法可以继承TestObject中的所有值/方法,而new的行为方式与PHP OO方式相同? (我确信JS的做法非常奇怪,好像我的大脑在大学Java中正确地为我提供了像PHP这样的工作)

解决方案

基本上

  TestObject2.prototype = new TestObject(); 

将TestObject的单个实例放在TestObject2的原型链中。因此,TestObject2的任何实例都将修改TestObject中的相同单实例后代属性。如果你在TestObject的construtor中放入另一个console.log,你会注意到它只被调用一次!



可以找到你确切问题的更多细节此处



<你需要在TextObject2的构造函数中调用TextObject的构造函数,如下所示:

  function TestObject2(data)
{
TestObject.call(this,data);
this .__ construct(data);
this.dothings = function()
{
this.dosomestuff();
}
}

通过调用TestObject构造函数并在(这是)新的TestObject2对象的范围,它在TestObject2对象中创建TestObject的所有元素。


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)

解决方案

Basically

TestObject2.prototype = new TestObject(); 

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:

function TestObject2(data)
{
    TestObject.call( this, data);
    this.__construct(data);
    this.dothings = function()
    {
        this.dosomestuff();
    }
}

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.

这篇关于javascript中的继承,“父”中的变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆