1158 字
6 分钟
06 · 原型与原型链
三个名字,一图说清
function Foo() {}const f = new Foo();
f.__proto__ === Foo.prototype // true —— 实例的"原型指针"Foo.prototype.constructor === Foo // true —— 原型指回构造函数Object.getPrototypeOf(f) === Foo.prototype // 同上,标准写法f ──__proto__──► Foo.prototype ──__proto__──► Object.prototype ──__proto__──► null ▲ │ .prototype Foo(函数对象本身还有自己的 __proto__ → Function.prototype)三条记忆要点:
- 只有函数有
prototype属性(它是”给实例用的原型对象”); - 每个对象都有
__proto__(内部[[Prototype]],标准 API 是Object.getPrototypeOf); prototype.constructor默认指回函数本身(可被覆盖,不要依赖它做判断)。
属性查找:先自己,再原型链
function Foo() { this.a = 1; }Foo.prototype.b = 2;
const f = new Foo();f.a; // 1 —— 自有属性f.b; // 2 —— 原型上的f.c; // undefined —— 整条链都没有查找规则:自身属性 → 原型 → 原型的原型 → … → null。
两个实际推论:
- 原型上的方法是共享的(所有实例指向同一个函数,省内存);
- 原型链越长,查找越慢(尤其是 miss 时要把整条链走完)。深层继承会影响热路径性能。
// 判断属性是"自有"还是"继承"Object.hasOwn(f, 'a'); // trueObject.hasOwn(f, 'b'); // falsein 会查整条链('b' in f 为 true),这也是 for...in 会遍历到继承属性的原因——遍历对象用 Object.keys/entries,别用 for...in。
手写 instanceof
原理:沿实例的原型链往上找,看能否命中构造函数的 prototype。
function myInstanceof(obj, Ctor) { if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) return false; let proto = Object.getPrototypeOf(obj); while (proto) { if (proto === Ctor.prototype) return true; proto = Object.getPrototypeOf(proto); } return false;}手写一遍就明白两个坑:
Object.create(null)的对象没有原型链,instanceof Object是false;- 跨 iframe/realm 会失效——不同窗口的
Array.prototype不是同一个对象,所以要用Array.isArray(它检查内部标记,不看原型)。
Object.create(null):干净字典
const dict = Object.create(null);dict.__proto__ = 'x'; // 只是一个普通属性,不会改原型Object.getPrototypeOf(dict); // null用途:做纯字典。避免 “键名撞上 toString/constructor” 这类问题:
const bad = {};bad['toString']; // 拿到函数(继承来的),不是 undefinedconst good = Object.create(null);good['toString']; // undefined ✅现代替代:直接用 Map(键可以是任意类型,还不受原型污染)。
原型污染:一个真实的安全问题
// 攻击者提交了这样的 JSON// { "__proto__": { "isAdmin": true } }
function merge(target, source) { for (const key in source) { if (typeof source[key] === 'object') { target[key] ??= {}; merge(target[key], source[key]); // ❌ 没过滤 __proto__ } else { target[key] = source[key]; } }}merge({}, JSON.parse(payload));({}).isAdmin; // true —— 所有对象都被"污染"了防护:
const FORBIDDEN = new Set(['__proto__', 'constructor', 'prototype']);function safeMerge(target, source) { for (const [k, v] of Object.entries(source)) { if (FORBIDDEN.has(k)) continue; if (v && typeof v === 'object' && !Array.isArray(v)) { target[k] = safeMerge(target[k] ?? {}, v); } else { target[k] = v; } } return target;}或者干脆用 Object.assign/structuredClone/Map,别自己写递归 merge。历史上 lodash 的 merge、jQuery 的 extend 都出过这类 CVE。
class 与原型的关系
class A { m() {} }// 等价于function A() {}A.prototype.m = function () {};
typeof A; // 'function'Object.getPrototypeOf(A.prototype) === Object.prototype; // trueclass 只是原型的语法糖,但有三个硬差别:
- 不能当函数调用(
A()报 TypeError); - 方法不可枚举(
Object.keys(A.prototype)是[]); - 默认严格模式。
常见坑
| 现象 | 根因 | 解法 |
|---|---|---|
| 改一个实例的属性影响了别的实例 | 属性挂在原型上(对象/数组) | 在构造函数里赋值,别放原型 |
for...in 遍历到多余属性 | 会走原型链 | 用 Object.keys |
instanceof 跨 iframe 失效 | 不同 realm 的构造函数不同 | 用 Array.isArray 等专用判断 |
obj.toString 判断出错 | 继承来的方法 | Object.hasOwn,或 Object.create(null) |
| 合并 JSON 后全局对象多了属性 | 原型污染 | 过滤 __proto__/constructor |
| 原型链太深导致慢 | 查找要走完整条链 | 减少继承层级,必要时扁平化 |
验收清单
- 能画出
实例 → 构造函数.prototype → Object.prototype → null的关系图 - 能说清
prototype与__proto__的区别 - 能手写
instanceof并解释跨 realm 为什么会失效 - 知道
Object.create(null)/Map在字典场景的用途 - 能识别并防御原型污染(至少知道要过滤哪三个键)
- 知道
class与原型的关系,以及三个硬差别
练习
- 手写 instanceof:实现后测试普通对象、
Object.create(null)对象、跨 iframe 数组三种输入。
自检:能解释后两种为什么结果”不符合直觉”。 - 复现原型污染:用上面那个
merge喂{"__proto__":{"isAdmin":true}},验证({}).isAdmin。
自检:能说出防护的两个位置(过滤键 + 用Object.entries而非for...in)。 - 观察原型共享:在原型上放一个数组,
new两个实例后 push,看另一个是否被影响。
自检:能说明”原型上的引用类型是共享的”,以及正确写法。