Last active
February 27, 2019 03:32
-
-
Save Maccauhuru/83052273fa2491f7f97edaa09958acda to your computer and use it in GitHub Desktop.
Getters & Setters Example 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductCost { | |
constructor(item,quantity,price) { | |
this.item = item; | |
this.quantity = quantity; | |
this.price = price; | |
} | |
//getter | |
get cost() { | |
return this.calcCost(); | |
} | |
// Method | |
calcCost() { | |
return `$${this.quantity * this.price}.00`; | |
} | |
} | |
const bread = new ProductCost("bread",2,3); | |
const milk = new ProductCost("milk",1,5); | |
bread.cost; //$6.00 | |
milk.cost;//$5.00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment