我不太确定为什么我的代码运行不正确..我想要做的是创建一个购物清单对象,它有几个函数来添加和删除项目.. 我可以实例化新项目的对象,但我的功能似乎不工作的原因。 如果你能救我头脑里剩下的几根头发,并告诉我问题在哪里,我将不胜感激。JavaScript对象函数不起作用
var groceryList = function(itemNames,quantity) {
if (Array.isArray(itemNames)) {
this.items = itemNames;
this.quantity = quantity
this.addItems = function(newItems){
if (Array.isArray(newItems)) {
this.items.concat(newItems);
} else {
console.log("Please enter the items in an array fashion!");
};
};
this.removeItem = function(name) {
var listSize = this.items.length;
for (var i = 0; i < listSize; i++) {
if (this.items[i] == name) {
this.items.splice(i,1);
break;
} else {
console.log("Please enter the items in an array fashion!")
};
};
};
} else {
console.log("Please enter the items in an array fashion!")
};
};
[ “CONCAT” 的可能重复不加入JavaScript数组?](http://stackoverflow.com/questions/12810366/concat-does-not-join-javascript-arrays-together) – 2014-10-19 05:55:52