简单总结一下几种继承的方法,简单易懂

1.原型链继承。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function Animal() {
        this.colors = ["black", "white"];
        this.color = "green";
      }
      Animal.prototype.getColor = function () {
        return this.color;
      };
      function Dog() {}
      // 将动物的构造函数new的对象赋值给Dog的原型
      Dog.prototype = new Animal();
      let dog1 = new Dog();
      dog1.colors.push("brown");
      let dog2 = new Dog();
      console.log(dog2.colors);
      console.log(dog2.getColor());
    </script>
  </body>
</html>

2.组合继承。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function Animal(name) {
        this.name = name;
        this.colors = ["blue", "green"];
      }
      Animal.prototype.getName = function () {
        return this.name;
      };
      //改变Dog的this指向让它指向Animal
      function Dog(name, age) {
        Animal.call(this, name);
        this.age = age;
      }

      Dog.prototype = new Animal();
      Dog.prototype.constructor = Dog;
      let dog1 = new Dog("夏天", 1);
      console.log(dog1.getName());
      dog1.colors.push("yellow");
      console.log(dog1);
      let dog2 = new Dog("冬天", 2);
      console.log(dog2);
    </script>
  </body>
</html>

3.寄生式组合继承

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function Animal(name) {
        this.name = name;
        this.colors = ["blue", "green"];
      }
      Animal.prototype.getName = function () {
        return this.name;
      };
      function Dog(name, age) {
        Animal.call(this, name);
        this.age = age;
      }

      Dog.prototype = Object.create(Animal.prototype);
      Dog.prototype.constructor = Dog;

      let dog1 = new Dog("冬天", 1);
      dog1.colors.push("red");
      console.log(dog1);
      console.log(dog1.getName());
      let dog2 = new Dog("秋天", 2);
      console.log(dog2);
      console.log(dog2.getName());
    </script>
  </body>
</html>

4.class类继承。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      class Animal {
        constructor(name) {
          this.name = name;
          this.colors = ["blue", "pink"];
        }
        getName() {
          return this.name + "" + this.age;
        }
      }
      class Dog extends Animal {
        constructor(name, age) {
          super(name);
          this.age = age;
        }
      }
      let dog1 = new Dog("小花", 12);
      let dog2 = new Dog("小为", 11);
      dog1.colors.push("hotpink");
      console.log(dog1);
      console.log(dog2);
    </script>
  </body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值