//es5其中的一种继承方法 let Animal = function (type){ this.type = type } //这是类的实例对象方法 Animal.prototype.eat = function (){ Animal.walk()//引用类的静态方法 console.log('eat food') } //这是类的静态方法 Animal.walk = function () { console.log('walking') } let Dog = function () { //初始化父类的构造函数 Animal.call(this,'dog') this.run = function () { console.log('i can run') } } Dog.prototype = Animal.prototype let dog = new Dog('dog') dog.eat()
//es6 class Animal { constructor (type) { this.type = type } //类的实例对象方法 eat (){ Animal.walk() console.log('eat food') } //类的静态方法 static walk (){ console.log('walking...') } } class Dog extends Animal { constructor (type) { super(type) this.age = 2 } } let dog = new Dog('dog') dog.eat()
Believe in yourself