15°

类的继承

//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(</span>'i can run'<span style="color: #000000;">)

}

}

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()

本文转载自博客园,原文链接:https://www.cnblogs.com/qjb2404/p/12206374.html

全部评论: 0

    我有话说: