2016-02-03 20 views
1

這是一個簡單的購物應用程序(有點兒)的功能,我編寫了這些功能來處理我的JS技能,並考慮到我的基本經驗。但一切正常,除了一件事:對象功能在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!") 
     } 
    } 

回答

1

調用applyStaffDiscount應做如下圖所示

cashRegister.applyStaffDiscount(name1); 

然後我n面applyStaffDiscount功能,它應該如下

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

嗨!Oxi!感謝編輯,我會盡快嘗試。另外,你能解釋爲什麼你改變它嗎?你知道,所以我會學會在將來避免這樣的錯誤。 編輯:它的工作!這個折扣正在被應用!謝謝你,奧西。我一直試圖弄清楚這一個星期。 –

+0

@ArhumIshtiaq:'name1'是局部變量,它只包含一個字符串(恰好是'employee object'的變量名)。現在再次在'applyStaffDiscount'函數內部,參數'employee'的字符串完全像變量'name1'。所以如果你想通過使用對象的名字訪問全局作用域('window')中的對象,你可以嘗試'window [employee]'。希望我解釋一下 – Oxi

+0

嘿!非常感謝!欣賞它! –

1

你沒有打電話給在這裏的功能:

cashRegister.applyStaffDiscount[name1] 

的是這樣試試:

cashRegister.applyStaffDiscount(name1); 
+0

這實際上是我從一開始就打算爲被訪問,但該程序保持返回一個未定義的值。 –