javascript中的继承,“父级”中的变量

发布于 2024-09-27 22:31:44 字数 1311 浏览 6 评论 0原文

我是第一次做 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别把无礼当个性 2024-10-04 22:31:44

基本上

TestObject2.prototype = new TestObject(); 

将 TestObject 的单个实例放置在 TestObject2 的原型链中。因此,TestObject2 的任何实例都将修改 TestObject 中相同的单个实例后代属性。如果您将另一个 console.log 放入 TestObject 的构造函数中,您会发现它只被调用一次!

您可以在此处找到有关您的确切问题的更多详细信息。

您需要从 TextObject2 的构造函数中调用 TextObject 的构造函数,如下所示:

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

通过调用 TestObject 构造函数并在新 TestObject2 对象的范围内执行它,它会在 TestObject2 对象中创建 TestObject 的所有元素。

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.

平安喜乐 2024-10-04 22:31:44

您是否已尝试将 this.clone_array = []; 定义到 TestObject2 中?

function TestObject2(data)
{
    this.clone_array = [];
    this.__construct(data);
    this.dothings = function()
    {
        this.dosomestuff();
    }
}

have you already tried to define this.clone_array = []; into TestObject2 instead?

function TestObject2(data)
{
    this.clone_array = [];
    this.__construct(data);
    this.dothings = function()
    {
        this.dosomestuff();
    }
}
咋地 2024-10-04 22:31:44

我认为

TestObject2.prototype = new TestObject();

是重写 TestObject2 的构造函数。

尝试

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


TestObject2.prototype = new TestObject();
TestObject2.prototype.constructor = TestObject2;

I think

TestObject2.prototype = new TestObject();

is overriding the constructor of TestObject2.

Try

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


TestObject2.prototype = new TestObject();
TestObject2.prototype.constructor = TestObject2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文