js实现继承的几种方式

1. 原型链继承

JavaScript中,每个函数都有一个prototype属性,这个属性是一个指向原型对象的指针。原型对象默认包含一个名为constructor的属性,指向原函数。当我们使用构造函数创建实例时,每个实例内部都有一个指向其构造函数原型对象的指针__proto__(在ES6中,通过Object.getPrototypeOf()方法访问)。如果原型对象中包含一个指向另一个类型的原型对象的指针,相应地,就形成了一个原型链。

复制代码
function Parent() {  
    this.name = 'parent';  
}  
  
Parent.prototype.sayName = function() {  
    console.log(this.name);  
};  
  
function Child() {  
    this.age = 10;  
}  
  
// Child继承Parent  
Child.prototype = new Parent();  
Child.prototype.constructor = Child;  
  
var child = new Child();  
child.sayName(); // 'parent'

2. 借用构造函数继承(伪造对象或经典继承)

在子类的构造函数中,通过调用父类的构造函数,并使用call()apply()方法,可以将父类的属性"借用"给子类实例。

复制代码
function Parent(name) {  
    this.name = name;  
}  
  
function Child(name, age) {  
    Parent.call(this, name); // 继承Parent的name属性  
    this.age = age;  
}  
  
var child = new Child('child', 5);  
console.log(child.name); // 'child'

3. 组合继承(原型链+借用构造函数继承)

组合继承是原型链继承和借用构造函数继承的组合,它结合了两种继承方式的优点,既可以通过原型链继承父类原型上的属性和方法,又可以通过构造函数继承父类的实例属性。

复制代码
function Parent(name) {  
    this.name = name;  
}  
  
Parent.prototype.sayName = function() {  
    console.log(this.name);  
};  
  
function Child(name, age) {  
    Parent.call(this, name); // 继承属性  
    this.age = age;  
}  
  
Child.prototype = new Parent(); // 继承方法  
Child.prototype.constructor = Child;  
  
Child.prototype.sayAge = function() {  
    console.log(this.age);  
};  
  
var child = new Child('child', 5);  
child.sayName(); // 'child'  
child.sayAge(); // 5

4. 原型式继承

原型式继承是借助原型可以基于已有的对象创建新对象,同时实现原型链继承。

复制代码
function object(o) {  
    function F() {}  
    F.prototype = o;  
    return new F();  
}  
  
var person = {  
    name: 'John',  
    friends: ['Sheldon', 'Leonard', 'Howard']  
};  
  
var anotherPerson = object(person);  
anotherPerson.name = 'Jane';  
anotherPerson.friends.push('Penny');  
  
console.log(person.friends); // ['Sheldon', 'Leonard', 'Howard', 'Penny']

5. 寄生式继承

寄生式继承是创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后返回对象。

复制代码
function createAnother(original) {  
    var clone = object(original); // 通过原型式继承获得原始对象的浅拷贝  
    clone.sayHi = function() { // 增强对象  
        console.log('hi');  
    };  
    return clone; // 返回增强后的对象  
}  
  
var person = {  
    name: 'John'  
};  
  
var anotherPerson = createAnother(person);  
anotherPerson.sayHi(); // 'hi'
相关推荐
guygg881 分钟前
二维弹塑性有限元分析(von Mises 等向硬化)— MATLAB 实现
开发语言·人工智能·matlab
在放️15 分钟前
Python 练习题讲解 2 · 循环计算
开发语言·python
江华森20 分钟前
高级 Bash 脚本编程指南 — 实战教程
开发语言·bash
我不是懒洋洋25 分钟前
【C++】string(string的成员变量、auto和范围for、string常用接口的说明、OJ题目、string的模拟实现)
c语言·开发语言·c++·visual studio
承渊政道26 分钟前
飞算JavaAI 智能引导背后的多 Agent 协作机制解析:从老旧 Java 后台升级到可运行工程
java·开发语言·spring boot·安全·intellij-idea·软件工程·ai编程
Brilliantwxx27 分钟前
【C++】 C++11 知识点梳理(中)
开发语言·c++
j7~30 分钟前
【C++】STL--Vector容器--拆析解剖Vector的实现以及Vector的底层详解(2)
开发语言·c++·动态二维数组·vector深度剖析·vector的实现·杨辉三角形
三品吉他手会点灯7 小时前
C语言学习笔记 - 50.流程控制4 - 流程控制为什么非常非常重要
c语言·开发语言·笔记·学习
武清伯MVP10 小时前
前端跨域方案大合集
前端·javascript
在放️10 小时前
Python 爬虫 · 第三方代理接入与合规使用
开发语言·爬虫·python