JavaScript是一门基于原型的编程语言,它的继承机制并不像传统面向对象语言那样通过类来实现,而是依靠原型链完成属性和方法的传递与复用。每个对象都有一个隐式的原型属性,构造函数则有显式的原型属性,两者共同构成了原型链的基础结构。

原型链的核心概念
要理解原型链,首先需要明确两个核心属性:prototype和__proto__。
prototype是函数特有的属性,每个函数在创建时都会自动生成一个prototype对象,这个对象的作用是存放所有实例共享的属性和方法。
__proto__是每个对象都有的隐式属性,它指向创建该对象的构造函数的prototype。当访问一个对象的属性时,如果该对象自身没有这个属性,就会沿着__proto__向上查找,直到找到null为止,这个查找路径就是原型链。
原型链的基本结构示例
我们可以通过一段简单的代码验证原型链的指向关系:
// 定义构造函数
function Person(name) {
this.name = name;
}
// 给构造函数的prototype添加方法
Person.prototype.sayName = function() {
console.log(this.name);
};
// 创建实例
const person1 = new Person('张三');
// 实例的__proto__指向构造函数的prototype
console.log(person1.__proto__ === Person.prototype); // true
// 构造函数prototype的__proto__指向Object.prototype
console.log(Person.prototype.__proto__ === Object.prototype); // true
// Object.prototype的__proto__指向null
console.log(Object.prototype.__proto__ === null); // true
// 调用实例自身没有的sayName方法,会沿着原型链查找到Person.prototype上的方法
person1.sayName(); // 输出:张三
常见的继承实现方式
1. 原型链继承
原型链继承是最基础的继承方式,核心是将子类的原型指向父类的实例,这样子类实例就能访问到父类实例的属性和父类原型上的方法。
// 父类构造函数
function Parent() {
this.parentProperty = '父类属性';
this.colors = ['red', 'blue'];
}
// 父类原型方法
Parent.prototype.getParentProperty = function() {
return this.parentProperty;
};
// 子类构造函数
function Child() {
this.childProperty = '子类属性';
}
// 核心:子类原型指向父类实例
Child.prototype = new Parent();
// 修复子类原型的constructor指向
Child.prototype.constructor = Child;
// 创建子类实例
const child1 = new Child();
console.log(child1.getParentProperty()); // 输出:父类属性
console.log(child1.parentProperty); // 输出:父类属性
// 原型链继承的缺点:引用类型属性会被所有实例共享
const child2 = new Child();
child1.colors.push('green');
console.log(child2.colors); // 输出:['red', 'blue', 'green']
2. 构造函数继承
构造函数继承通过在子类构造函数中调用父类构造函数,利用call或apply改变父类的this指向,解决原型链继承中引用类型属性共享的问题。
// 父类构造函数
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue'];
}
// 子类构造函数
function Child(name, age) {
// 调用父类构造函数,将this指向子类实例
Parent.call(this, name);
this.age = age;
}
// 创建子类实例
const child1 = new Child('张三', 18);
const child2 = new Child('李四', 20);
child1.colors.push('green');
console.log(child1.colors); // 输出:['red', 'blue', 'green']
console.log(child2.colors); // 输出:['red', 'blue']
console.log(child1.name); // 输出:张三
// 缺点:父类原型上的方法无法被子类继承
Parent.prototype.sayName = function() {
console.log(this.name);
};
child1.sayName(); // 报错:sayName is not a function
3. 组合继承
组合继承结合了原型链继承和构造函数继承的优点,既可以让实例拥有自己的属性,又能共享父类原型上的方法,是实际开发中最常用的继承方式之一。
// 父类构造函数
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue'];
}
// 父类原型方法
Parent.prototype.sayName = function() {
console.log(this.name);
};
// 子类构造函数
function Child(name, age) {
// 构造函数继承:继承父类实例属性
Parent.call(this, name);
this.age = age;
}
// 原型链继承:继承父类原型方法
Child.prototype = new Parent();
// 修复constructor指向
Child.prototype.constructor = Child;
// 子类原型添加自己的方法
Child.prototype.sayAge = function() {
console.log(this.age);
};
// 测试
const child1 = new Child('张三', 18);
const child2 = new Child('李四', 20);
child1.colors.push('green');
console.log(child1.colors); // 输出:['red', 'blue', 'green']
console.log(child2.colors); // 输出:['red', 'blue']
child1.sayName(); // 输出:张三
child1.sayAge(); // 输出:18
不同继承方式的对比
我们可以通过表格直观对比几种常见继承方式的特点:
| 继承方式 | 优点 | 缺点 |
|---|---|---|
| 原型链继承 | 实现简单,父类原型方法可复用 | 引用类型属性被所有实例共享,无法向父类构造函数传参 |
| 构造函数继承 | 实例属性独立,可向父类传参 | 父类原型方法无法继承,函数无法复用 |
| 组合继承 | 属性独立、方法复用,可传参 | 父类构造函数会被调用两次 |
ES6 class继承的实现原理
ES6引入的class语法本质是原型链继承的语法糖,它的继承逻辑和组合继承类似,只是写法更简洁。
// 父类
class Parent {
constructor(name) {
this.name = name;
this.colors = ['red', 'blue'];
}
sayName() {
console.log(this.name);
}
}
// 子类,通过extends实现继承
class Child extends Parent {
constructor(name, age) {
// super调用父类构造函数,相当于Parent.call(this, name)
super(name);
this.age = age;
}
sayAge() {
console.log(this.age);
}
}
// 测试
const child1 = new Child('张三', 18);
const child2 = new Child('李四', 20);
child1.colors.push('green');
console.log(child1.colors); // 输出:['red', 'blue', 'green']
console.log(child2.colors); // 输出:['red', 'blue']
child1.sayName(); // 输出:张三
child1.sayAge(); // 输出:18
理解原型链和继承机制,能帮助开发者更好地掌握JavaScript的对象模型,在编写组件、封装工具函数时更合理地设计代码结构,避免不必要的属性污染和逻辑错误。
JavaScript原型链继承机制prototype__proto__修改时间:2026-07-07 23:12:31