Javascript 使用prototype函数将新创建的整个对象推送到数组
我试图在构建对象时运行一个函数,将整个对象传递给一个函数,然后将其推送到数组中 我可以一次通过一个属性,如下所示:Javascript 使用prototype函数将新创建的整个对象推送到数组,javascript,function,object,properties,prototype,Javascript,Function,Object,Properties,Prototype,我试图在构建对象时运行一个函数,将整个对象传递给一个函数,然后将其推送到数组中 我可以一次通过一个属性,如下所示: function Item(title, description, price) { 'use strict'; this.title = title; this.description = description; this.getPrice = function (price) { var pricestring = "£
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);
}
this.addToPage(this.Item);
Item.prototype.addToPage = function (item) {
'use strict';
constructedItems.push(item);
};
但我希望能够像这样通过整个物体:
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);
}
this.addToPage(this.Item);
Item.prototype.addToPage = function (item) {
'use strict';
constructedItems.push(item);
};
但是,它总是返回未定义的
我的addToPage是这样的函数:
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);
}
this.addToPage(this.Item);
Item.prototype.addToPage = function (item) {
'use strict';
constructedItems.push(item);
};
真的有可能从物体内部传递整个物体吗?或者我可以使用另一种技术吗?您正在寻找的
这个,它指的是正在构造的对象。是否确实可以从对象内部传递整个对象?
是的,在您的情况下,整个对象就是这个。但实际上您不想传递它,因为您只需要几个属性。