简体   繁体   English

使用原型功能将新创建的整个对象推入数组

[英]Pushing newly created whole object to array with prototype function

I am trying to run a function on the construction of my objects which passes the whole object to a function, which then pushes it to an array. 我试图在我的对象的构造上运行一个函数,该函数将整个对象传递给一个函数,然后将其推入数组。

I can pass one property at a time like this: 我可以一次传递一个属性:

    function Item(title, description, price) {
    'use strict';
    this.title = title;
    this.description = description;
    this.getPrice = function (price) {
        var pricestring = "£";
        pricestring = pricestring + price;
        return pricestring;
    };
    this.addToPage(this.title, this.description);
}

But I would like to be able to pass the whole object like this: 但是我希望能够像这样传递整个对象:

this.addToPage(this.Item);

however it always returns undefined. 但是它总是返回undefined。

My addToPage is function is like this: 我的addToPage是函数是这样的:

Item.prototype.addToPage = function (item) {
    'use strict';
    constructedItems.push(item);
};

Is it actually possible to pass the whole object from inside the object? 实际上有可能从对象内部传递整个对象吗? or is there another technique I could use? 还是我可以使用另一种技术?

您正在寻找this ,它是指正在构造的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM