Skip to content

Instantly share code, notes, and snippets.

@belocer
Created February 9, 2021 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belocer/3a62fa973407f36da48a81aefba19cb9 to your computer and use it in GitHub Desktop.
Save belocer/3a62fa973407f36da48a81aefba19cb9 to your computer and use it in GitHub Desktop.
ES6 пример наследования
// ES6
class ProductES {
constructor(brand, price, discount) {
this.brand = brand;
this.price = price;
this.discount = discount;
}
get brand() {
return this._brand;
}
set brand(newName) {
this._brand = newName;
}
sum() {
return (this.price * (100 - this.discount)) / 100;
}
static plus(x, y) {
return x + y;
}
}
class CustomES extends ProductES {
constructor(brand, price, discount, day) {
super(brand, price, discount);
this.day = day;
}
getBrandAndDiscount () {
return super.sum() + this.price;
}
}
const newProduct = new ProductES('Samsung', 200, 10);
const newCustom = new CustomES('Apple', 20000, 10, 'Wednesday');
console.log(newProduct);
console.log(newProduct.sum());
console.log(ProductES.plus(24500, 79500));
console.log(newCustom.day)
console.log(newCustom.price)
console.log(newCustom.getBrandAndDiscount())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment