javascript - 对象构造函数语法错误

标签 javascript

在学习 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/

相关文章:

运行setTimeout方法+传递变量时Javascript堆栈溢出

javascript - 花式框不打开 iFrame 第二次

javascript - 单击时更改图像

javascript - react : setInterval Not working after one interval

javascript - 如何修改KendoUI DropDownList项

javascript - 找不到 javascript 库的 ruby​​-toolbox 等价物

javascript - React Native Navigator renderView 返回条件不合逻辑

javascript - 如何从codeigniter日历的ajax成功结果中获取元素的attr?

javascript - AngularJS中的页面完成事件

javascript - 使用 Jest 在另一个函数中测试一个函数