Created
February 9, 2021 13:22
-
-
Save belocer/3a62fa973407f36da48a81aefba19cb9 to your computer and use it in GitHub Desktop.
ES6 пример наследования
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
// 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