How to call a function by passing an object name?


#1

In the code of this lesson, I have made a function to call the discount from an object. but I cannot let the function recognize the object:

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

var me = new StaffMember("S", 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;
},
// Create a new method applyStaffDiscount here
applyStaffDiscount: function(employee) {
this.total -= this.total * (employee.disountPercent / 100);
}

};

cashRegister.applyStaffDiscount(me);

In the last code, I used both me as it is or "me" with quotes, it still cannot work well. Could anyone help me to figure out what I can do with this problem please? Thank you!


#2

@sanly123654,

Have a close look at

employee.disountPercent


#3

Figured it! Thanks a lot!!