Skip to content

Instantly share code, notes, and snippets.

@Maccauhuru
Last active February 27, 2019 03:32
Show Gist options
  • Save Maccauhuru/83052273fa2491f7f97edaa09958acda to your computer and use it in GitHub Desktop.
Save Maccauhuru/83052273fa2491f7f97edaa09958acda to your computer and use it in GitHub Desktop.
Getters & Setters Example 3
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