js几种继承方式

1.原型链继承
2.借用构造函数继承
3.组合继承
4.拷贝继承

这些都不完美 最完美的如下

  function Animal(name, age) {
    this.name = name;
    this.age = age;
  };
  Animal.prototype.eat = function () {
    console.log('eat something');
  };

  function Dog(name, age, blood) {
    Animal.call(this, name, age);
    this.blood = blood;
  };

    function F() {
    //定义空函数
  }
  F.prototype = Animal.prototype;
  let f = new F();
  Dog.prototype = f;
  Dog.prototype.constructor = Dog;
  Dog.prototype.say = function () {
    console.log('wangwang');
  }
  let animal = new Animal('ljj',22)
  let erha = new Dog('erha', 1, '哈士奇');
  console.log(erha.name, erha.blood);
  erha.eat();
  erha.say();
  console.log(erha instanceof Dog,erha instanceof Animal)
  console.dir(erha)
  console.dir(animal)

猜你喜欢

转载自blog.csdn.net/qaqLjj/article/details/85175374