继承

119 阅读1分钟

两种继承 一

function Person(name,age){
    //call 继承 这种继承方式只能继承私有属性;
    Animal.call(this,1000);
    //把Animal 中的this 换成当前的实例,把1000传给了Animal.
    this.name=name;
    this.age=age;
}
 Person.prototype=Object.create(Animal.prototype)
 //cerate创造了一个空对象,空对象的__proto__指向了Animal的prototype
 Person.prototype.eat=function(){
     console.log("吃")
 }
 function Anima(iq){
     this.iq=iq;
 }
     Animal.qqq = 123;
    Animal.prototype.move = function(){
        console.log('能动')
    }
    Animal.prototype.sleep = function(){
        console.log('休息')
    }

  class Perent(){
      constructor(){
          this.name="parent"
          this.age=age
      }
      say(){
            console.log('hello parent')
      }
        static qqq = 245

  }
  class Child extends Parent{
      constructor(){
          super()//写了extends 和constructor 则必写super()
          super其实就是Parent的constructor
          this.money=100
      }
      plat(){
          console.log('child play')
      }
  }
    var xm = new Child();
    console.log(xm);