javascript - 对象函数在 JS 中不起作用

标签 javascript function oop object functional-programming

这是一个简单的购物应用程序(有点)的功能,考虑到我在其中的基本经验,我编写了它来提高我的 JS 技能。但除了一件事之外,一切都有效:

applyStaffDiscount 函数似乎不起作用。每次我尝试应用员工折扣时,该程序都会返回与尝试应用折扣之前存在的相同原始值。请帮助并让我知道如何改进这个程序。

function StaffMember(name, discountPercent) {
    this.name = name;
    this.discountPercent = discountPercent;
}

var sally = new StaffMember("sally", 5);
var bob = new StaffMember("bob", 10);
var arhum = new StaffMember("arhum", 20);

var cashRegister = {
    total: 0,
    lastTransactionAmount: 0,
    add: function(itemCost) {
        this.total += (itemCost || 0);
        this.lastTransactionAmount = itemCost;
    },
    scan: function(item, quantity) {
        switch (item) {
            case "eggs": this.add(0.98 * quantity); break;
            case "milk": this.add(1.23 * quantity); break;
            case "magazine": this.add(4.99 * quantity); break;
            case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    voidLastTransaction: function() {
        this.total -= this.lastTransactionAmount;
        this.lastTransactionAmount = 0;
    },
    applyStaffDiscount: function(employee) {
        this.total -= (this.total * (employee.discountPercent/100))
    }
};

    var cont = confirm("Are you ready to shop?")
    while (cont) {
        var user = ("Choose your function: A to Add Item, ED for Employee Discount, VT to Void Transaction or just X to Close")
        var askUser = prompt(user).toUpperCase()
        if (askUser === "A") {
            var itemName = prompt("What item would you like?", "Eggs, Milk, Magazine, Chocolate").toLowerCase()
            var itemNum = prompt("How many?")
            cashRegister.scan(itemName, itemNum)
            var cont = confirm("Your total bill is $" + cashRegister.total.toFixed(2) + ". Would you like anything else?")
        }
        else if (askUser === "ED") {
            var name1 = prompt("Please enter you name").toLowerCase()
            cashRegister.applyStaffDiscount[name1]
            alert("Your total bill is $" + cashRegister.total.toFixed(2) + ".")
}
        else if (askUser === "VT") {
            cashRegister.voidLastTransaction()
            alert("Your previous transaction has been voided. Your total bill is $" + cashRegister.total.toFixed(2) + " now.")
        }
        else if (askUser === "X") {
            cont = false
        }
        if (cont === false) {
            alert("We hope you had a good time. have a nice day!")
        }
    }

最佳答案

调用 applyStaffDiscount 应如下所示完成

cashRegister.applyStaffDiscount(name1);

然后在applyStaffDiscount函数中,应按如下方式访问它

this.total -= (this.total * (window[employee].discountPercent/100))

关于javascript - 对象函数在 JS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35176784/

相关文章:

javascript - 我需要angularjs中没有数组括号的输出值

javascript - Jquery ajax 在 Firefox 中不起作用

javascript - Express Nodejs - 如何将 request.files 传递给另一个 POST 请求

php - 为什么一个变量在类中可以是静态的而不能是全局的

android - 数据库驱动应用程序中的 OOP 建模对象?

JAVA OOP继承树

javascript - 页面打开一段时间后使用 jquery-ui 方法速度极慢

function - 纯函数可以调用外部函数吗?

javascript - 使用随机选择从数组返回函数值

c - 使用函数更改指针包含的地址