0
Why say discount is not defined?
In practice 34.2 I get the error that discount is not defined, why? function main() { var prodID = readLine(); var price = parseInt(readLine(),10); var discount = parseInt(readLine(),10); var prod1= new Product(prodID, price); console.log(prod1.prodID + " price: " + prod1.price); prod1.changePrice(discount); console.log(prod1.prodID + " new price: " + prod1.changePrice); } function Product(prodID, price) { this.prodID = prodID; this.price = price; this.discount = discount; this.changePrice = function(){ return this.price - (this.price * (this.discount / 100)) } };
2 Réponses
+ 1
discount is only defined in main, not in Product. This priciple is called scope
0
You have to define discount outside the main function because it's locally defined in main and the product function has no access to this variable.