一、先分清三个核心属性
-
prototype:函数才拥有 ,是构造函数的显式原型,是一个对象。实例不具备prototype。 -
__proto__:对象(实例)才拥有 ,隐式原型,指向构造函数的prototype。浏览器访问器属性,开发不推荐直接修改。 -
constructor:原型对象上的属性,默认指向构造函数本身。// 定义构造函数
function Person(name) {
this.name = name;
}
// 给构造函数原型添加方法
Person.prototype.sayHi = function () {
console.log(你好,我是${this.name});
}const p1 = new Person("小明");
p1.sayHi();console.log(Person.prototype);
console.log(p1.proto === Person.prototype); // true
console.log(Person.prototype.constructor === Person); // true
一句话总结:实例的 proto 指向构造函数的 prototype
二、new 关键字到底做了什么?
new 创建实例,内部隐式执行 4 步:
- 创建一个空的简单 JavaScript 对象
{} - 给空对象设置
__proto__,指向构造函数的prototype - 绑定 this,执行构造函数代码,给新对象添加属性
- 如果构造函数没有手动 return 对象,默认返回这个新对象
手写简易 new,加深理解:
function myNew(Constructor, ...args) {
const obj = {};
obj.__proto__ = Constructor.prototype;
const result = Constructor.apply(obj, args);
return typeof result === 'object' && result !== null ? result : obj;
}
const p2 = myNew(Person, "小红");
p2.sayHi();
三、原型链是什么?
当我们访问一个对象的属性 / 方法时:
-
先在自身对象上查找,找到就直接使用
-
找不到,就沿着
__proto__往上找,也就是去它的原型对象找 -
一直往上,直到
Object.prototype -
Object.prototype.__proto__是null,查找终止,返回undefined这条向上查找的链路,就是原型链。console.log(p1.toString());
// toString 不在 p1、不在 Person.prototype,在 Object.prototype
原型链示意图文字版:
p1实例 → Person.prototype → Object.prototype → null
四、基于原型实现继承
1. 原型继承(古老写法,有缺陷)
function Student(name, score) {
this.name = name;
this.score = score;
}
Student.prototype = new Person();
Student.prototype.constructor = Student;
const s1 = new Student("小李", 90);
s1.sayHi();
缺陷:引用类型属性会被所有实例共享,一改全改。
2. 组合继承(原型 + 构造函数借用,经典方案)
function Student(name, score) {
Person.call(this, name); // 借用父构造函数,继承实例属性
this.score = score;
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
Student.prototype.showScore = function () {
console.log(`我的分数:${this.score}`);
}
const s2 = new Student("小张", 95);
s2.sayHi();
s2.showScore();
优点:解决引用类型共享问题;缺点:父构造函数执行两次。
3. ES6 class 语法糖(日常开发最常用)
class 本质还是原型,只是包装成类写法,底层没有改变原型机制:
class Person {
constructor(name) {
this.name = name;
}
sayHi() {
console.log(`你好,我是${this.name}`);
}
}
class Student extends Person {
constructor(name, score) {
super(name); // 调用父类构造函数,必须写在前面
this.score = score;
}
showScore() {
console.log(`分数:${this.score}`);
}
}
const s3 = new Student("阿泽", 88);
s3.sayHi();
s3.showScore();
五、开发高频踩坑点
-
不要直接修改
__proto__,性能差,推荐Object.create()创建指定原型的对象 -
给原型追加方法,不要直接覆盖
prototype,容易丢失constructor// ❌ 不好
Student.prototype = {
show(){}
}
// ✅ 推荐
Student.prototype.show = function(){} -
原型上放共享方法,实例自身放独有属性,节省内存
-
hasOwnProperty():判断属性是不是对象自身属性,不会去原型链查找console.log(p1.hasOwnProperty('name')); // true
console.log(p1.hasOwnProperty('sayHi')); // false -
in运算符:只要原型链上能找到属性,就返回 true,区分不开自身和原型属性console.log('sayHi' in p1); // true
六、原型相关常用 API 汇总
-
Object.create(proto):创建一个新对象,指定它的原型 -
Object.getPrototypeOf(obj):安全获取对象的原型(替代__proto__) -
Object.setPrototypeOf(obj, proto):设置对象原型,谨慎使用 -
instanceof:判断构造函数的prototype是否出现在实例的原型链上console.log(s3 instanceof Student); // true
console.log(s3 instanceof Person); // true
console.log(s3 instanceof Object); // true
七、总结
- JS 没有传统类,是原型继承,所有对象都有原型
prototype属于函数,__proto__属于实例对象- 属性查找沿着原型链向上,终点是
null - ES6
class extends只是语法糖,底层依旧是原型链 - 开发优先使用
class,但是一定要懂原型原理,不然遇到instanceof、继承问题、框架源码时看不懂