Skip to content

Instantly share code, notes, and snippets.

@CarsonSlovoka
Created November 29, 2022 08:40
Show Gist options
  • Select an option

  • Save CarsonSlovoka/d6580c9f07c342fbdaf3c1b7094765c7 to your computer and use it in GitHub Desktop.

Select an option

Save CarsonSlovoka/d6580c9f07c342fbdaf3c1b7094765c7 to your computer and use it in GitHub Desktop.
ES6 Class Multiple inheritance
<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