文章
问答
冒泡
JavaScript的继承(页面刷新,数十行文字消失,下面粘贴复制自己在VS Code上写的的)

    // 借用构造函数结成v
    // function Person (age, name){
    //  this.age = age
    //  this.name = name
    // }
    // Person.prototype.sayName = function(){
    //  console.log(this.name)
    // }
    // function Man (age, name, gender) {
    //  Person.apply(this, arguments)
    //  this.gender = gender
    // }
    // const Man1 = new Man(18,'lx', 'boy')
    // Man1.sayName()
    // console.log(Man1)
    // 原型链继承
    // function DogFather(){
    //  this.name = 'dogFname'
    // }
    // DogFather.prototype.sayName = function(){
    //  alert(this.name)
    // }
    // function Dog (name, age){
    //  this.name = name
    //  this.age = age
    // }
    // Dog.prototype = new DogFather()
    // const dog1 = new Dog('wangwang', 18)
    // dog1.sayName()
    // console.log(dog1)

    // 组合继承
      // function DogF (name){
      //  this.name = name 
      // }
      // DogF.prototype.sayName = function(){
      //  alert(this.name)
      // }
      // function DogSon(name, age){
      //  DogF.apply(this, arguments)
      //  this.age = age
      // }
      // DogSon.prototype = new DogF()
      // const smallDog = new DogSon('smallDog', 1)
      // console.log(smallDog)
      // smallDog.sayName()

      // // 寄生组合继承
      // function Dogf(age, name){
      //  this.age = age
      //  this.name = name 
      // }
      // Dogf.prototype.sayName = function(){
      //  alert(this.name)
      // }
      // //继承父类属性
      // function Dogs(){
      //  Dogf.apply(this, arguments)
      //  //可以写自己的属性
      // }
      // // 继承父类方法 利用IIFE立即执行函数
      // (function(){
      //  let Suber = function(){}
      //  Suber.prototype = Dogf.prototype
      //  Dogs.prototype = new Suber()
      // }())
      // // 解决构造器指向问题 否则指向的父类构造器
      // console.log(Dogs.prototype.constructor)
      // Dogs.prototype.constructor = Dogs
      // console.log(Dogs.prototype.constructor)
      // let smallDog = new Dogs('smallWangWang', 0.5)
      // console.log(smallDog)
      // smallDog.sayName()

      // class类及继承
      class DogF{
        constructor(nameage){
          this.name = name
          this.age = age
        }
        sayName(){
          console.log(this.name)
        }
        static lover = 'juju' // 静态属性 只有构造函数能够使用
        static sayYourLover(){
          alert(this.lover)
        }
      }
      const bigDog = new DogF('大汪汪'18)
      // console.log(DogF.lover)
      DogF.sayYourLover()
      // console.log(bigDog.lover) // undefined 实例上没有静态属性
      // bigDog.sayYourLover()  // 报错 实力上没有静态方法
      // console.log(bigDog)
      bigDog.sayName()
      // 继承
      class SmallDog extends DogF{
        constructor(namegender){
          super(name// 使用构造器来增加自己的属性 必须先super父类的属性 否则报错
          this.gender = gender
        }
        sayGender(){
          console.log(this.gender)
        }
      }
      const SmallDog1 = new SmallDog('zhuzhu''coolbigboy')
      console.log(SmallDog1)
      SmallDog1.sayGender()// 使用自己的方法
      SmallDog1.sayName()// 使用父类的方法


关于作者

xlynn
获得点赞
文章被阅读