JavaScript实现继承的6种方式详解
本文系统梳理JavaScript实现继承的6种核心方式,包括原型链、借用构造函数、组合继承、原型式、寄生式及寄生组合式继承。详细解析各模式的实现原理、优缺点及适用场景,重点推荐寄生组合式继承为最理想的引用类型继承范式,帮助开发者根据实际需求选择最佳方案。
JavaScript继承概述
本文通过具体实例,详细介绍JavaScript实现继承的6种方式,供读者参考学习。
原型链继承
1、【原型链继承】其实现本质是重写原型对象,将其替换为新类型的实例。实际上并非SubType原型的constructor属性被直接重写,而是SubType的原型指向了另一个对象——即SuperType的原型,且该原型对象的constructor属性指向SuperType。
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
//继承了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function(){
return this.subproperty;
}
var instance = new SubType();
alert(instance.getSuperValue());//true
[注意1]定义方法时需格外谨慎,向原型添加方法的代码必须放置在替换原型的语句之后。
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
//继承了SuperType
SubType.prototype = new SuperType();
//添加了新方法
SubType.prototype.getSubValue = function(){
return this.subproperty;
}
//重写超类型的方法
SubType.prototype.getSuperValue = function(){
return false;
}
var instance = new SubType();
alert(instance.getSuperValue());//false
[注意2]通过原型链实现继承时,禁止使用对象字面量创建原型方法,因为这样做会中断并重写原有的原型链。
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
//继承了SuperType
SubType.prototype = new SuperType();
//使用字面量方法添加新方法会导致上一行代码无效
SubType.prototype = {
getSubValue : function(){
return this,subproperty;
},
someOtherMethod : function(){
return false;
}
};
var instance = new SubType();
alert(instance.getSuperValue());//error
[缺点1]在创建子类型实例时,无法向超类型的构造函数中传递参数。 [缺点2]包含引用类型值原型属性会被所有实例共享,导致数据污染。
function SuperType(){
this.colors = ['red','blue','green'];
}
function SubType(){}
//继承了SuperType
SubType.prototype = new SuperType();
var instance1 = new SubType();
instance1.colors.push('black');
alert(instance1.colors);//'red,blue,green,black'
var instance2 = new SubType();
alert(instance2.colors);//'red,blue,green,black'
借用构造函数继承
2、【借用构造函数继承】(又称伪造对象或经典继承):在子类型构造函数的内部调用超类型构造函数。通过apply()和call()方法,可以在新创建的对象上执行超类型构造函数,从而实现实例属性的继承。
function SuperType(){
this.colors = ['red','blue','green'];
}
function SubType(){
//继承了SuperType
SuperType.call(this);
}
var instance1 = new SubType();
instance1.colors.push('black');
alert(instance1.colors);//'red,blue,green,black'
var instance2 = new SubType();
alert(instance2.colors);//'red,blue,green'
[优点]支持向超类型构造函数传递参数。
function SuperType(name){
this.name = name;
}
function SubType(){
//继承了SUperType,同时还传递了参数
SuperType.call(this,"Nicholas");
//实例属性
this.age = 29;
}
var instance = new SubType();
alert(instance.name);//"Nicholas"
alert(instance.age);//29
[注意]为确保SuperType构造函数不会覆盖子类型的属性,应在调用超类型构造函数之后,再添加子类型中需要定义的属性。
function SuperType(name){
this.name = name;
this.age = 30;
}
function SubType(){
//实例属性
this.age = 29;
//继承了SUperType,同时还传递了参数
SuperType.call(this,"Nicholas");
}
var instance = new SubType();
//实例属性被重写为SuperType构造函数的属性
alert(instance.age);//30
组合继承
[缺点1]无法实现函数复用,因为方法必须在构造函数中定义。 [缺点2]超类型原型中定义的方法对子类型不可见,导致所有类型只能使用构造函数模式,无法利用原型链的优势。 3、【组合继承】(又称伪经典继承):结合原型链和借用构造函数技术,发挥二者优势。利用原型链实现对原型属性和方法的继承,通过借用构造函数实现对实例属性的继承。这种模式既实现了函数复用,又保证每个实例拥有独立属性,是JavaScript中最常用的继承模式。
function SuperType(name){
this.name = name;
this.colors = ['red','blue','green'];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name,age){
//继承属性
SuperType.call(this,name);
this.age = age;
}
//继承方法
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
alert(this.age);
}
var instance1 = new SubType("Nicholas",29);
instance1.colors.push("black");
alert(instance1.colors);//'red,blue,green,black'
instance1.sayName();//"Nicholas"
instance1.sayAge();//29
var instance2 = new SubType("Greg",27);
alert(instance2.colors);//'red,blue,green'
instance2.sayName();//"Greg"
instance2.sayAge();//27
[缺点]无论何种情况,都会调用两次超类型构造函数:一次在创建子类型原型时,另一次在子类型构造函数内部。子类型最终包含超类型对象的全部实例属性,但必须在调用子类型构造函数时重写这些属性,造成性能浪费。
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name,age){
SuperType.call(this,name); // 第二次调用SuperType()
this.age = age;
}
SubType.prototype = new SuperType(); //第一次调用SuperType()
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
alert(this.age);
};
原型式继承
4、【原型式继承】借助原型基于已有对象创建新对象,无需创建自定义类型。本质上,object()函数对传入的对象执行了一次浅复制。 [注意]原型式继承要求必须存在一个作为基础的对象。若存在此类对象,可将其传递给object()函数,随后根据具体需求对得到的对象进行修改。
function object(o){
function F(){};
F.prototype = o;
return new F();
}
var person = {
name: "Nicholas",
friends: ["Shelby","Court","Van"]
};
var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
alert(person.friends);//"Shelby,Court,Van,Rob,Barbie"
【4.1】【Object.create()方法】:ECMAScript5新增的Object.create()方法规范化了原型式继承。该方法接收两个参数:一个用作新对象原型的对象,以及(可选的)一个为新对象定义额外属性的对象。当只传入一个参数时,Object.create()的行为与object()方法相同。
function object(o){
function F(){};
F.prototype = o;
return new F();
}
var person = {
name: "Nicholas",
friends:["Shelby","Court","Van"]
};
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
alert(person.friends);//"Shelby,Court,Van,Rob,Barbie"
[注意]Object.create()方法的第二个参数格式与Object.defineProperties()方法的第二个参数相同:每个属性均通过自己的描述符定义。以这种方式指定的任何属性都会覆盖原型对象上的同名属性。
var person = {
name: "Nicholas",
friends:["Shelby","Court","Van"]
};
var anotherPerson = Object.create(person,{
name: {
value: "Greg"
}
});
alert(anotherPerson.name);//"Greg"
【4.2】低版本浏览器下兼容Object.create()方法的实现方案。
if(typeof Object.create != "function"){
(function(){
var F = function(){};
Object.create = function(o){
if(arguments.length > 1){
throw Error('Second argument noe supported');
}
if(o === null){
throw Error("Cannot set a null [[Prototype]]");
}
if(typeof o != 'Object'){
throw TypeError("Arguments must be an object");
}
F.prototype = o;
return new F();
}
})();
}
寄生式继承
5、【寄生式继承】创建一个仅用于封装继承过程的函数,该函数在内部以某种方式增强对象,最后再像执行所有工作一样返回对象。 [缺点]无法实现函数复用。
function object(o){
function F(){};
F.prototype = o;
return new F();
}
function createAnother(original){
var clone = object(original);//通过调用函数创建一个新对象
clone.sayHi = function(){ //以某种方式来增强这个对象
alert("hi");
};
return clone;//返回这个对象
}
var person = {
name: "Nicholas",
friends: ["Shelby","Court","Van"]
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();//"hi"
寄生组合式继承
6、【寄生组合式继承】通过借用构造函数继承属性,通过原型链的混成形式继承方法。其核心思路是:无需为了指定子类型的原型而调用超类型的构造函数,所需仅为超类型原型的一个副本。本质上,即使用寄生式继承来继承超类型的原型,再将结果指定给子类型的原型。寄生组合式继承是引用类型最理想的继承范式。
//这个例子中的高效率体现在它只调用了一次Super构造函数,并且因此避免了在SubType.prototype上面创建不必要的、多余的属性。与此同时,原型链还能保持不变。
function object(o){
function F(){};
F.prototype = o;
return new F();
}
function inheritPrototype(subType,superType){
var prototype = object(superType.prototype);//创建对象
prototype.constructor = subType;//增强对象
subType.prototype = prototype;//指定对象
}
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name,age){
SuperType.call(this,name);
this.age = age;
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge = function(){
alert(this.age);
}
以上就是本文的全部内容,关于javascript实现继承的方式,感谢大家的阅读,小编会再接再厉!
ES6模块化中js文件怎么用import和export交互
2026-11-05 17:00:48
JS读取本地文件时报错或无响应的常见原因
2026-11-05 16:32:31
let、const与var的区别:JavaScript变量声明最佳实践
2026-11-05 14:16:38
JS中直接定义静态二维数组的字面量写法
2026-11-05 14:16:08
JS 怎么查找字符串中所有匹配项的位置
2026-11-05 14:15:53
