继承

2023-11-26  本文已影响0人  輪徊傷

1、借助原型是实现继承

实现原理: 让子类的原型对象指向父类实例,当子类实例找不到对应的属性和方法时,就会往它的原型对象,也就是父类实例上找,从而实现对父类的属性和方法的继承

        function Parent() {
            this.name = "parent"
            this.arr = [1, 2, 3]
        }
        function Child() {
            this.type = "child"
        }
        Child.prototype = new Parent()
        var c1 = new Child()
        var c2 = new Child()

缺点一、所有实例都会共享父类实例的属性。(原型上的属性是共享的,一个实例修改了原型属性,另一个实例的原型属性也会被修改!!)

        c1.arr.push(4)
        console.log(c1, c2)

缺点二、在创建子类型的实例时,无法向父类型的构造函数传递参数。

2、构造函数实现继承

实现原理: 在子类的构造函数中执行父类的构造函数,并为其绑定子类的this,让父类的构造函数把成员属性和方法都挂到子类this上去 ( call、apply )

function Parent() {
    this.name = ["LB", "ipanda"];
}
function Child() {
    Parent.call(this)
    this.type = "child"
}
解决了原型链继承 所有实例都会共享父类实例的属性的缺点
let c1 = new Child()
c1.name.push("babay")
console.log(c1.name);

let c2 = new Child()
console.log(c2.name);
解决了原型链继承 在创建子类型的实例时,无法向父类型的构造函数传递参数。
        function Parent(name) {
            this.name = name;
        }
        function Child(name) {
            Parent.call(this, name)
        }
        let c1 = new Child(["LB", "ipanda"])
        c1.name.push("babay")
        console.log(c1.name);

缺点一、只继承了父类构造函数的属性,没有继承父类原型的属性

        function Parent() {
            this.name = ["LB", "ipanda"];
        }
        Parent.prototype.myFunc = function () { }
        function Child() {
            Parent.call(this)
        }
        let c1 = new Child()
        console.log(c1.name);
        console.log(c1.myFunc);

缺点二、无法实现父类构造函数的复用,在创建子类型的实例时都要调用父类构造函数

        let num = 0;
        function Parent() {
            console.log(`被调用次数${++num}`);
        }
        function Child() {
            Parent.call(this)
        }
        let c1 = new Child()
        let c2 = new Child()
        let c3 = new Child()

缺点三、每个子类实例都会拷贝一份父类构造函数中的方法,作为实例自己的方法,比如 myFunc()

  1. 每个实例都拷贝一份,占用内存大,尤其是方法过多的时候。
  2. 方法都作为了实例自己的方法,当需求改变,要改动其中的一个方法时,之前所有的实例,他们的该方法都不能及时作出更新。只有后面的实例才能访问到新方法。
function Parent() {
    this.nameList = ["LB", "ipanda"];
    this.myFunc = function(){
        console.log("Parent  !!!");
    }
}
function Child() {
    Parent.call(this)
}
let c1 = new Child()
console.log(c1);

3、组合方式实现继承

结合了两种模式的优点:使用借用构造函数的技术实现实例属性的继承,使用原型链实现原型属性和方法的继承。

function Parent() {
    this.name = "parent"
    this.arr = [1, 2, 3]
}
Parent.prototype.myFunc = function(){
    console.log("MyFunction");
}
function Child() {
    Parent.call(this)
    this.type = "child"
}
Child.prototype = new Parent()
var c1 = new Child()
c1.arr.push(4)
console.log(c1);

缺点:(1)调用了两次父类构造函数(耗内存),一次是在创建子类型原型时,另一次是在子类型构造函数内部;(2)创建的实例和原型上存在两份相同的属性(耗内存)

let num = 0;
function Parent() {
    console.log(`被调用次数${++num}`);
    this.name = "parent"
    this.arr = [1, 2, 3]
}
Parent.prototype.myFunc = function(){
    console.log("MyFunction");
}
function Child() {
    Parent.call(this)//第二次调用
    this.type = "child"
}
Child.prototype = new Parent()//第一次调用
Child.prototype.construcotr = Child
var c1 = new Child()
c1.arr.push(4)
console.log(c1);

3、寄生组合式继承

let num = 0;
function Parent() {
    console.log(`被调用次数${++num}`);
    this.name = "parent"
    this.arr = [1, 2, 3]
    function hello(){
      console.log("hello")
  }
}
Parent.prototype.myFunc = function () {
    console.log("MyFunction");
}
function Child() {
    Parent.call(this)
    this.type = "child"
}
Child.prototype = Object.create(Parent.prototype)
Child.prototype.construcotr = Child
var c1 = new Child()
c1.arr.push(4)
console.log(c1)

ES6的继承

class People{
        constructor(name,age){
             this.name = name
             this.age = age
        }
    eat(){
    }
}
class Student extends People{
    constructor(id,name,age){
        super(name,age)
        this.id = id
    }
}
上一篇 下一篇

猜你喜欢

热点阅读