继承

作者: 輪徊傷 | 来源:发表于2023-11-26 10:53 被阅读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
    }
}

相关文章

  • 继承 继承

    属性拷贝 继承不单单能通过原型链实现,也能通过其他方式实现,属性拷贝就是其中一种方法。 通过属性拷贝也能实现继承子...

  • 继承(单继承,多继承)

    将共性的内容放在父类中,子类只需要关注自己特有的内容 python中所有的内容都是对象,所有的对象都直接或间接继承...

  • js继承方式

    类式继承 构造函数继承 组合继承 类式继承 + 构造函数继承 原型式继承 寄生式继承 寄生组合式继承 寄生式继承 ...

  • Python-学习之路-08 OOP -02

    单继承和多继承 单继承:每个类只能继承一个类 多继承:每个类可以继承多个类 单继承的多继承的优缺点 菱形继承/钻石...

  • 原型相关(二)

    1.继承 继承方式:接口继承(只继承方法签名)实现继承(继承实际的方法)ECMAScript只支持实现继承,并且主...

  • 继承

    继承的引入和概述 继承案例和继承的好处 继承的弊端 Java中继承的特点 继承的注意实现和什么时候使用继承 继承中...

  • Java面向对象三大特性之继承

    继承 一、继承的特点 Java只支持单继承单继承 多继承 单继承、多继承优缺点①单继承优点:提高了代码的复用性,让...

  • 7、面向对象的程序设计3(《JS高级》笔记)

    三、继承 许多OO语言都支持两种继承方式:接口继承和实现继承。接口继承只继承方法签名,而实现继承则继承实际方法。由...

  • 【重学前端】JavaScript中的继承

    JavaScript中继承主要分为六种:类式继承(原型链继承)、构造函数继承、组合继承、原型式继承、寄生式继承、寄...

  • js之继承

    文章主讲 JS 继承,包括原型链继承、构造函数继承、组合继承、寄生组合继承、原型式继承、 ES6 继承,以及 多继...

网友评论

      本文标题:继承

      本文链接:https://www.haomeiwen.com/subject/ystpxrtx.html