在学习 JS 的过程中......距离 JS 第一天已经是第二周了,我在语法上遇到了很大的问题
function Customer(name, street, city, state, email, balance) {
this.name = name;
this.street = street;
this.city = city;
this.state = state;
this.email = email;
this.balance = balance;
this.payDownBal = function(amtPaid) {
this.balance -= amtPaid;
};
this.addToBa = function(amtCharged) {
this.balance += amtCharged;
};
}
var cust2 = new Customer("Sally Smith", "234 Main ", "Pittsburgh", "PA", "ssmith@aol.com", 0.00);
cust2.addToBal(15.50);
Customer.prototype.isCreditAvail = true;
Customer.prototype.toString = function() {
return this.name + " lives at " + this.street + " in " +
this.city + " " + this.state + " email : " + this.email +
" and has a balance of $ " + this.balance.toFixed(2) + "Creditworty : " + this.isCredAvail;
}
document.write(cust2.toString());
我找不到错误...可以帮助我吗?
最佳答案
你犯了一个简单的错误。 您定义了 addToBa() 函数,但正在调用未定义的 addToBal() 函数。
更改第 17 行以调用函数 addToBa()。 (或将函数声明更改为addToBal())。
关于javascript - 对象构造函数语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35234244/