Skip to content

Instantly share code, notes, and snippets.

@belocer

belocer/index.js

Created Feb 9, 2021
Embed
What would you like to do?
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