我目前正在学习JavaScript和所有测试通过,但我的某个函数似乎没有正常运行。JavaScript for-loop不返回项目,测试不通过
这里的测试
it("should get record by its id", function(){
customer1.setFunds(100);
customer1.buy(record1, store1);
customer1.buy(record2, store1);
var item = customer1.getRecord("xyz123");
console.log(customer1);
is.equal("Nirvana", item.artist);
}),
这里的对象
record2 = new Record("Nirvana", "In Utero", 25, 11, "xyz123");//the last attribute is the id
这是我正在测试
getRecord: function(id){
for(var i = 0; i<this.boughtItems.length; i+=1){
if(this.boughtItems[i].id === id){
return this.boughtItems[i];
}else{
return "The item doesn't exist";
}
}
函数的一点是,this.boughtItems由该元素的我我正在寻找和功能无法返回它。我知道JS对象有时会以一种奇怪的方式工作,但这对我来说很模糊。除非我是盲人,看不到一个简单的问题去那里
谢谢!
更新记录不成立分配给它的任何功能,只是属性
var Record = function(artist, title, price, stock, id){
this.artist = artist;
this.title = title;
this.price = price;
this.stock = stock;
this.id = id;
};
UPDATE2买入()方法
buy: function(product, store){
if(this.funds >= product.price){
store.sell(product);
var itemToBuy = new Record(product.artist, product.title, product.price, 1, product.id);
this.boughtItems.push(itemToBuy);
this.funds -= itemToBuy.price;
}else{
return "You cannot afford to buy this item";
}
}
,这是更加古怪,在我的测试中, “商品”对象显示为“您买不起这个商品”
我们可以看到您正在使用的'Record'原型类吗? – forrestmid
更新:)谢谢! – bwielk
我们可以在Customer上看到'buy()'方法吗? –