我堆積了一個數組的總和。代碼是波紋管JavaScript總和函數
function User(name,email) {
this.name = name;
this.email = email;
this.cartAmount = [];
this.total = 0;
}
User.prototype = {
constructor: User,
addCart: function(mrp){
this.cartAmount.push(mrp);
},
changeEmail: function(newmail){
this.email = newmail;
},
showCart: function() {
var cart = this.cartAmount.length >0 ? this.cartAmount.join("tk,") : "No product in the cart";
return this.name+" has "+cart+" in his cart.";
},
intotal: function(){
for(var n in this.cartAmount){
this.total += this.cartAmount[n];
return this.total;
}
}
};
athar= new User("Athar Jamil", "[email protected]");
console.log(athar.name);
athar.changeEmail("[email protected]");
console.log(athar.email);
athar.addCart(20);
athar.addCart(50);
athar.addCart(80);
console.log(athar.intotal());
它顯示我只有20作爲總和的結果。問題是什麼?
回報是進行早期人類! –
這是一個熱門網絡問題? – immibis