ES6中类的继承

2019-11-5 张宇童 学习日志

相比ES5中类的继承ES6中类的继承语法更加简单明了


class Animal {
    constructor(type) {
        this.type = type
    }
   eat () {
        console.log(`我是small ${this.type},我要吃`)
    }
}

class Dog extends Animal {}

let dog = new Dog('dog')

dog.eat() // 输出 我是small dog,我要吃


在看下面一个example

class Animal {
    constructor(type) {
        this.type = type
    }
    eat () {
        console.log(`我是small ${this.type},我要吃`)
    }
}

class Dog extends Animal {
    constructor(type, age) {
        super(type)
        this.age = age
    }
    say() {
        console.log(`我快${this.age}岁了`)
    }
}
let dog = new Dog('dog', 2)
dog.eat()
dog.age()

标签: javascript ES6

发表评论:

Powered by emlog