Created
November 29, 2022 08:40
-
-
Save CarsonSlovoka/d6580c9f07c342fbdaf3c1b7094765c7 to your computer and use it in GitHub Desktop.
ES6 Class Multiple inheritance
This file contains hidden or 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
| <script> | |
| class Meta { | |
| static id = 0 // 流水號 | |
| static GetID() { | |
| return ++Meta.id | |
| } | |
| constructor() { | |
| console.log("meta created.") | |
| // this.id = Meta.id++ // 先給值後流水後也++ | |
| this.id = Meta.GetID() | |
| } | |
| GetID() { | |
| return this.id | |
| } | |
| } | |
| class Animal extends Meta { | |
| constructor(species) { | |
| super() | |
| this.species = species | |
| } | |
| Type() { | |
| return this.species | |
| } | |
| } | |
| class Person extends Animal { | |
| constructor(name, age) { | |
| super("human") | |
| this.name = name | |
| this.age = age | |
| } | |
| Hello() { | |
| console.log(`[${this.Type()}] ${this.name}: Hello`) | |
| } | |
| } | |
| for (const curP of [new Person("Carson", 30), new Person("foo", 18)]) { | |
| curP.Hello() | |
| console.log(curP.GetID()) | |
| console.log(curP.Type()) | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment