class 被叫了十年「语法糖」,但它有三处不是糖:方法不可枚举、体内强制严格模式、不能不带 new 调用。这篇把 class 完整拆成 ES5 逐项对齐,顺带说清 prototype / __proto__ / constructor 三个名字的关系,以及 super 到底绑的是什么 ------ 它不绑父类,绑的是「这个方法挂在谁身上」。
统计日期 :2026-11-26 | 基准环境 :Node 22.22.2(Windows 11) 观测手段 :
Object.getPrototypeOf/Object.getOwnPropertyDescriptor读结构;Object.setPrototypeOf主动断开原型链做对照;new.target与Symbol.hasInstance验证语义 本文性质:所有结构结论均以属性描述符与身份比较得出,不依赖「看起来对」;凡涉及抛错的代码块显式标注严格模式
引子:class 的方法为什么不在 Object.keys 里
先看两个「应该完全等价」的写法:
js
class Person {
constructor(name) { this.name = name; }
greet() { return 'hi ' + this.name; }
}
function Manual(name) { this.name = name; }
Manual.prototype.greet = function () { return 'hi ' + this.name; };
console.log(`class 版 Object.keys → ${JSON.stringify(Object.keys(Person.prototype))}`);
console.log(`手写版 Object.keys → ${JSON.stringify(Object.keys(Manual.prototype))}`);
console.log(`class 版 getOwnPropertyNames → ${JSON.stringify(Object.getOwnPropertyNames(Person.prototype))}`);
输出:
css
class 版 Object.keys → []
手写版 Object.keys → ["greet"]
class 版 getOwnPropertyNames → ["constructor","greet"]
greet 在两边的原型上都存在(getOwnPropertyNames 都列出来了),但 Object.keys 只看得见手写版的那个。
差别是枚举性 :class 定义的方法 enumerable: false,Ctor.prototype.fn = ... 这种方式定义的是 enumerable: true。
这一条就推翻了「class 是纯语法糖」。下面从头拆。
一、四个名字,各是什么
学原型链最容易卡住的地方,是四个名字混在一起:prototype、[[Prototype]]、__proto__、constructor。先把它们分开:
| 名字 | 是什么 | 谁有 |
|---|---|---|
prototype |
一个普通属性,值是个对象 | 只有函数(除箭头函数、方法简写外) |
[[Prototype]] |
一个内部槽,指向「我的原型」 | 所有对象 |
__proto__ |
访问 [[Prototype]] 的访问器(getter/setter) |
继承自 Object.prototype |
constructor |
原型对象上的反向指针,指回构造函数 | Ctor.prototype 上自动有 |
一句能把它们串起来的话:prototype 是「我造出来的孩子该认谁当原型」,[[Prototype]] 是「我自己认谁当原型」。 前者是给别人的,后者是自己的。
用代码验证这层关系:
js
function Foo(name) { this.name = name; }
const f = new Foo('a');
console.log(`Foo.prototype.constructor === Foo → ${Foo.prototype.constructor === Foo}`);
console.log(`Object.getPrototypeOf(f) === Foo.prototype → ${Object.getPrototypeOf(f) === Foo.prototype}`);
console.log(`f.__proto__ === Foo.prototype → ${f.__proto__ === Foo.prototype}`);
console.log(`Object.getPrototypeOf(Foo) === Function.prototype → ${Object.getPrototypeOf(Foo) === Function.prototype}`);
console.log(`typeof f.prototype → ${typeof f.prototype}`);
console.log(`'prototype' in f → ${'prototype' in f}`);
输出:
javascript
Foo.prototype.constructor === Foo → true
Object.getPrototypeOf(f) === Foo.prototype → true
f.__proto__ === Foo.prototype → true
Object.getPrototypeOf(Foo) === Function.prototype → true
typeof f.prototype → undefined
'prototype' in f → false
五条结论:
Foo.prototype.constructor === Foo------ 原型对象上自动有一个constructor指回构造函数。它只是个普通属性,不是不可变的:Foo.prototype.constructor = 别的能改,改完instanceof不受影响(instanceof不看constructor)。- 实例的
[[Prototype]]就是Ctor.prototype------ 这是new干的第二件事。 f.__proto__与Object.getPrototypeOf(f)等价 ------__proto__只是Object.prototype上的一个访问器。- 函数自己的
[[Prototype]]是Function.prototype------ 因为函数也是对象,它是Function的实例。 - 实例上没有
prototype这个属性 ------typeof f.prototype是undefined。prototype只挂在函数上,是给new用的模板。
判据:区分「给孩子的」和「自己的」------Ctor.prototype 是给实例的模板,Object.getPrototypeOf(x) 是 x 自己的原型。 混用这两个词,后面所有推理都会歪。
一条推论:__proto__ 不是标准属性名,是遗留的访问器 。它能用,但规范推荐 Object.getPrototypeOf / Object.setPrototypeOf。用 Object.create(null) 造出来的对象连 __proto__ 都没有(因为它不从 Object.prototype 继承)。
二、new 到底做了四件事
new Foo(args) 的完整语义是四步:
- 创建一个新对象
- 把这个对象的
[[Prototype]]设为Foo.prototype - 以这个新对象为
this,执行Foo的函数体 - 如果
Foo返回了一个对象,就用那个对象;否则返回第 1 步创建的新对象
第 4 步是最容易记错的一步。return 什么会被采纳,什么会被忽略:
js
function ReturnsNothing() { this.a = 1; }
function ReturnsObj() { this.a = 1; return { b: 2 }; }
function ReturnsPrim() { this.a = 1; return 42; }
function ReturnsNull() { this.a = 1; return null; }
const v = new ReturnsNothing();
const o = new ReturnsObj();
const p = new ReturnsPrim();
const n = new ReturnsNull();
console.log(`无 return → x.a = ${v.a}`);
console.log(`return 对象 → 'a' in x: ${'a' in o},'b' in x: ${'b' in o}`);
console.log(`return 原始值 → x.a = ${p.a}`);
console.log(`return null → x.a = ${n.a}`);
console.log(`无 return 的实例 instanceof 构造函数 → ${v instanceof ReturnsNothing}`);
输出:
kotlin
无 return → x.a = 1
return 对象 → 'a' in x: false,'b' in x: true
return 原始值 → x.a = 1
return null → x.a = 1
无 return 的实例 instanceof 构造函数 → true
只有第二种被取代了:return { b: 2 } 让 this 上写的 a 白写,整个对象被换掉。
判据:new 只在构造函数返回「对象」时才丢弃 this;返回原始值、null、undefined 一律忽略。 所以 return null 和 return 42 都不会毁掉实例 ------ 很多人以为 return null 会让 new 返回 null,实际不会。
这条规则的实用价值:构造函数里「提前退出」不能靠 return,只能靠 return this 或抛错 。想返回一个完全不同的对象(比如工厂模式),return 才有效。
三、原型链的终点与层数
一个 class Child extends Parent 的实例,往上走是几层?逐层验证:
js
class Parent { static kind() { return 'Parent'; } }
class Child extends Parent {}
console.log(`Object.getPrototypeOf(Child.prototype) === Parent.prototype → ${Object.getPrototypeOf(Child.prototype) === Parent.prototype}`);
console.log(`Object.getPrototypeOf(Child) === Parent → ${Object.getPrototypeOf(Child) === Parent}`);
console.log(`Child.kind() → ${Child.kind()}`);
console.log(`Child 自己有 kind → ${Object.prototype.hasOwnProperty.call(Child, 'kind')}`);
const inst = new Child();
const l1 = Object.getPrototypeOf(inst);
const l2 = Object.getPrototypeOf(l1);
const l3 = Object.getPrototypeOf(l2);
const l4 = Object.getPrototypeOf(l3);
console.log(`第 1 层是 Child.prototype → ${l1 === Child.prototype}`);
console.log(`第 2 层是 Parent.prototype → ${l2 === Parent.prototype}`);
console.log(`第 3 层是 Object.prototype → ${l3 === Object.prototype}`);
console.log(`第 4 层是 null → ${l4 === null}`);
输出:
javascript
Object.getPrototypeOf(Child.prototype) === Parent.prototype → true
Object.getPrototypeOf(Child) === Parent → true
Child.kind() → Parent
Child 自己有 kind → false
第 1 层是 Child.prototype → true
第 2 层是 Parent.prototype → true
第 3 层是 Object.prototype → true
第 4 层是 null → true
从实例出发是四层,终点是 null。
注意第 3 行的 Child.kind() ------ 静态方法也能「继承」,但不是沿实例的原型链 。Child 自己有 kind 吗?没有(第 4 行)。它是沿 Child.__proto__ === Parent 找到的(第 2 行)。这是第五节的主题。
判据:原型链的终点是 null,不是 Object.prototype。 所以「一直往上找」的循环必须以 p !== null 结束,而不是以「找到 Object.prototype」结束 ------ 后者会漏掉 Object.prototype 自己的方法(hasOwnProperty 就在那一层)。
四、class 不是纯语法糖:三条证据
第一节点出了第一条(不可枚举)。另外两条:
js
class StrictBody {
check() { return this === undefined; }
}
const detached = new StrictBody().check;
try { console.log(`解构后调用 → this 是 undefined:${detached()}`); }
catch (e) { console.log(`解构后调用 → ${e.constructor.name}`); }
class NotCallable { }
try { NotCallable(); console.log('NotCallable() → 没抛'); }
catch (e) { console.log(`NotCallable() → ${e.constructor.name}`); }
function NormalFn() { return this; }
console.log(`普通函数直接调用 → this 是 globalThis:${NormalFn() === globalThis}`);
输出:
javascript
解构后调用 → this 是 undefined:true
NotCallable() → TypeError
普通函数直接调用 → this 是 globalThis:true
三处「不是糖」汇总:
| 差异 | class |
function 构造函数 |
|---|---|---|
| 原型方法的枚举性 | enumerable: false |
enumerable: true |
| 体内严格性 | 强制严格模式 (解构调用得到 undefined) |
跟随外层(非严格下是 globalThis) |
无 new 调用 |
TypeError: Class constructor ... cannot be invoked without 'new' |
正常执行,污染 this |
判据:class 与「构造函数 + 原型赋值」在结构上可以完全对齐(第八节给出对齐的写法),但在默认行为上不是糖 ------ 它顺手把三个现代默认值塞了进去。
第二条特别值得记:类方法体内的 this 在解构调用时是 undefined,所以会抛 TypeError: Cannot read properties of undefined。 看到这个报错,第一反应应该是「这个方法被解构了」,而不是「参数有问题」------这条与本系列《this 的四种绑定》里那条判据是同一件事的两面。
五、extends 设的是两条链
大多数人以为 extends 只做一件事(让子类实例的原型链经过父类原型)。实际上它设两条:
| 链 | 效果 |
|---|---|
Child.prototype.__proto__ = Parent.prototype |
实例方法继承:child.greet() 能找到 Parent.prototype.greet |
Child.__proto__ = Parent |
静态方法继承:Child.create() 能找到 Parent.create |
第三节已经验证了两条都是 true,以及 Child.kind() 能拿到父类静态方法但不在自有属性上。
第二条容易被忽略,但它解释了两个常见现象:
现象一:class Child extends Array,Child.of(...) 直接可用。 因为 Child.__proto__ === Array,of 是 Array 的静态方法。
现象二:class Foo extends null 之后 Foo() 不行、new Foo() 也不行。 因为 Foo.__proto__ = null,连 Function.prototype 上的 bind / call 都没有了。
判据:extends 同时改「实例的原型链」和「构造函数自己的原型链」。 前者管实例方法,后者管静态方法 ------ 两条链各管一半,漏掉任何一条都会得出错误结论。
一个边界:extends 右边可以是任意「有 prototype 的构造器」,也可以是 null。如果是普通函数(非 class),super() 会调用它;如果返回原始值,super() 会抛错(因为 class 的 this 必须由一个对象初始化)。
六、super 走 [[HomeObject]],不是静态绑定父类
这是全文最容易搞错的一条。
直觉上 super.m() 应该等于「调用父类的 m」。但如果父类的原型关系被改了呢?做一次主动破坏:
js
class A { m() { return 'A.m'; } }
class B extends A { m() { return `B.m > ${super.m()}`; } }
const b = new B();
const before = b.m();
Object.setPrototypeOf(B.prototype, Object.prototype); // 断开 B.prototype → A.prototype
let after;
try { after = b.m(); } catch (e) { after = e.constructor.name; }
console.log(`super.m() 断开前:${before}`);
console.log(`super.m() 断开后:${after}`);
class C extends A { m() { return `C.m > ${A.prototype.m.call(this)}`; } }
const c = new C();
Object.setPrototypeOf(C.prototype, Object.prototype);
console.log(`A.prototype.m.call(this) 同样破坏下:${c.m()}`);
class D { who() { return this.tag || 'no-tag'; } }
class E extends D { who() { return `E sees ${super.who()}`; } }
const e = new E();
e.tag = 'instance';
console.log(`super.who() 里的 this:${e.who()}`);
输出:
javascript
super.m() 断开前:B.m > A.m
super.m() 断开后:TypeError
A.prototype.m.call(this) 同样破坏下:C.m > A.m
super.who() 里的 this:E sees instance
第 2 行是关键:断开 B.prototype 的原型之后,super.m() 直接抛 TypeError。 如果 super 是「静态绑定到父类」,它应该照样能找到 A.m ------ 但事实是找不到。
第 3 行是对照组:A.prototype.m.call(this) 直接钉死在 A.prototype 上,同样的破坏下毫发无损。
判据:super.m() 的语义是「在 [[HomeObject]] 的当前原型上找 m」,其中 [[HomeObject]] 是「这个方法定义时挂在哪个对象上」。 它不记录「父类是谁」,只记录「我挂在哪」,然后实时去查那一层的原型。
由此可以推出两个实用结论:
super是动态的 ------ 改原型会影响已定义方法的super行为。这既是灵活性(mixin 依赖这一点),也是坑(热更新时改原型可能让super失效)。super.m()里的this是调用者 (第 4 行的E sees instance),不是[[HomeObject]]。所以super.m()不会丢失调用者状态,本质等价于「用当前this调用原型上的方法」。
对象字面量里也能用 super,此时 [[HomeObject]] 是那个字面量对象本身 ------ 这也是 __proto__: 写法里能调「父对象方法」的原因。
七、instanceof 的算法
instanceof 做的事比想象的简单:取构造函数的 prototype,然后在左侧对象的原型链上找它。手写一遍就清楚了:
js
class A {}
class B extends A {}
const b = new B();
function myInstanceof(obj, Ctor) {
const target = Ctor.prototype;
let p = Object.getPrototypeOf(obj);
while (p !== null) {
if (p === target) return true;
p = Object.getPrototypeOf(p);
}
return false;
}
console.log(`b instanceof A:${b instanceof A}`);
console.log(`手写算法与内置一致:${myInstanceof(b, A) === (b instanceof A) && myInstanceof(b, Array) === (b instanceof Array)}`);
console.log(`Object.create(F.prototype) 骗过 instanceof:${Object.create(A.prototype) instanceof A}`);
class Weird { static [Symbol.hasInstance](x) { return x === 42; } }
console.log(`42 instanceof Weird:${42 instanceof Weird}`);
输出:
javascript
b instanceof A:true
手写算法与内置一致:true
Object.create(F.prototype) 骗过 instanceof:true
42 instanceof Weird:true
三条推论:
instanceof不看constructor,只看原型链。 手写算法里根本没有constructor这个词,结果与内置完全一致。Object.create(Ctor.prototype)能骗过instanceof------ 没有经过构造函数、没有自有属性,但原型对得上,instanceof就认。所以instanceof不能当作「是不是 new 出来的」的证据。- 可以被
Symbol.hasInstance完全接管 ------ 第 4 行的42 instanceof Weird为true,因为Weird覆写了这个符号方法,原型链压根没参与。
判据:instanceof 是「原型链上有没有这个 prototype」的语法糖,不是类型系统。 跨 realm(iframe、vm、Worker)会失效,因为各自的 Error.prototype 是不同的对象 ------ 这条在本系列《Error.isError 与 using》里有完整推导,不重复。
一个边界:如果右侧不是函数,或者函数没有 prototype(箭头函数、Proxy 包的方法),instanceof 抛 TypeError: Function has non-object prototype 'undefined' in instanceof check。
八、把 class 拆成 ES5:五项逐一对齐
如果 class 真是糖,那么一定能写成完全等价的 ES5。能写,但要写五项:
js
'use strict';
// 手写等价物
function Person(name) {
if (!new.target) throw new TypeError('Class constructor Person cannot be invoked without new');
this.name = name;
}
Object.defineProperty(Person.prototype, 'greet', {
value: function greet() { return 'hi ' + this.name; },
writable: true, enumerable: false, configurable: true,
});
Object.defineProperty(Person, 'create', {
value: function create(n) { return new Person(n); },
writable: true, enumerable: false, configurable: true,
});
// 真 class
class RealPerson {
constructor(name) { this.name = name; }
greet() { return 'hi ' + this.name; }
static create(n) { return new RealPerson(n); }
}
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
const desc = (o, k) => {
const d = Object.getOwnPropertyDescriptor(o, k);
return [d.enumerable, d.writable, d.configurable].join('/');
};
console.log(`方法不可枚举一致:${same(Object.keys(Person.prototype), Object.keys(RealPerson.prototype))}`);
console.log(`自有属性名一致:${same(Object.getOwnPropertyNames(Person.prototype), Object.getOwnPropertyNames(RealPerson.prototype))}`);
console.log(`greet 描述符一致:${desc(Person.prototype, 'greet') === desc(RealPerson.prototype, 'greet')}`);
console.log(`静态方法描述符一致:${desc(Person, 'create') === desc(RealPerson, 'create')}`);
const t1 = (() => { try { Person('x'); return 'no'; } catch (e) { return e.constructor.name; } })();
const t2 = (() => { try { RealPerson('x'); return 'no'; } catch (e) { return e.constructor.name; } })();
console.log(`无 new 调用都抛同类错:${t1 === t2 && t1 === 'TypeError'}`);
输出:
arduino
方法不可枚举一致:true
自有属性名一致:true
greet 描述符一致:true
静态方法描述符一致:true
无 new 调用都抛同类错:true
五项对齐清单:
| # | 要对齐什么 | 手写时怎么写 |
|---|---|---|
| 1 | 方法不可枚举 | Object.defineProperty + enumerable: false(直接赋值会给 true) |
| 2 | 原型上要有 constructor |
function 声明自带,不用管 |
| 3 | 描述符三属性 | 显式写 writable: true, enumerable: false, configurable: true |
| 4 | 静态方法也不可枚举 | 同样用 defineProperty,挂在构造函数上 |
| 5 | 无 new 时抛错 |
开头加 if (!new.target) throw new TypeError(...) |
| 6 | 体内严格模式 | 文件或函数顶部加 'use strict' |
判据:判断「两个函数是否等价」,不能只看「调用结果一样」,要把属性描述符逐项对齐。 第 1 项和第 4 项最容易被漏 ------ 因为枚举性不影响任何「调用」,只影响 Object.keys / for...in / 展开 / JSON.stringify。这就是「行为一致但结构不同」最典型的例子。
第六项还牵出一个必须显式处理的问题:CommonJS 模块默认是 非严格模式 ,不加 'use strict' 就得不到 class 的严格语义。所以上面这个块的第一行必须是 'use strict';,少这一行,第 5 项和第 6 项都会失效。
九、什么时候用原型,什么时候用 class
class 已经覆盖了 99% 的场景,但还有三类地方绕不开原型:
| 场景 | 用什么 | 原因 |
|---|---|---|
| 定义类型、写继承 | class |
语法清晰、super 可用、静态继承自动 |
| 只要一个「临时对象继承另一个对象」 | Object.create(proto) |
不需要构造函数,也不必造中间类 |
| 需要动态增删方法、mixin | Object.assign(Ctor.prototype, mixin) |
class 体不能动态加 |
| 造纯字典(防原型污染) | Object.create(null) |
没有任何原型方法,__proto__ 也不存在 |
真的需要「无 new 也能调」的工厂 |
普通函数 | class 不允许 |
| 需要把「父类方法」当值传递 | 显式 A.prototype.m |
super 不是值,无法赋给变量 |
最后一条值得展开:super 是语法,不是对象。两种用法都会失败,而且失败方式不同:
js
// 一、把 super 本身当值取出来 ------ 语法错误
try {
new Function('class A {}; class B extends A { m() { const s = super; } }');
console.log('const s = super → 没抛');
} catch (e) {
console.log(`const s = super → ${e.constructor.name}`);
}
// 二、把 super.m 取出来再调用 ------ 能取到,但 this 丢了
class A { m() { return this.tag || 'no-tag'; } }
class B extends A {
m() {
const f = super.m;
return f();
}
}
try {
console.log(`取出来再调用 → ${new B().m()}`);
} catch (e) {
console.log(`取出来再调用 → ${e.constructor.name}`);
}
输出:
javascript
const s = super → SyntaxError
取出来再调用 → TypeError
第一种在解析期 就死了(super 不能出现在赋值右侧)。第二种能通过解析,但 super.m 取出来的是 A.prototype.m 这个普通函数,脱离 super 的上下文后 this 变成 undefined(class 体内严格),于是 this.tag 炸掉。
所以要么写 super.m() 完整形式,要么显式 A.prototype.m.call(this, ...)。
判据:super 只能在方法体内以 super.x 或 super[x] 的形式使用,它不是可传递的引用。 需要传递父类方法时,用 A.prototype.m 并自己 call。
关于「class 能不能被 Proxy 包装」:能,但要注意本系列 B-10 那条 ------ Proxy 丢的是内部槽,而 class 的实例没有特殊内部槽,所以包装后方法调用正常;但 #私有字段 走的是 [[PrivateGet]],代理后必挂。
十、一页速查
javascript
四个名字
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
prototype 函数上的普通属性,给 new 用的模板
[[Prototype]] 每个对象都有,指向「我的原型」
__proto__ 访问 [[Prototype]] 的访问器(遗留)
constructor 原型上的反向指针,指回构造函数(可改,instanceof 不看它)
new 的四步
────────────────────────────────────────────
① 创建新对象
② 设 [[Prototype]] = Ctor.prototype
③ 以新对象为 this 执行构造函数
④ 构造函数返回对象 → 用它;返回原始值 / null / undefined → 忽略
原型链层数(class Child extends Parent 的实例)
────────────────────────────────────────────
实例 → Child.prototype → Parent.prototype → Object.prototype → null(4 层)
class 不是纯语法糖(三条)
────────────────────────────────────────────
方法不可枚举 Object.keys(C.prototype) → []
体内强制严格 解构调用时 this 是 undefined
不能无 new 调用 TypeError: Class constructor ... cannot be invoked without 'new'
extends 设两条链
────────────────────────────────────────────
Child.prototype.__proto__ = Parent.prototype 实例方法继承
Child.__proto__ = Parent 静态方法继承
super 的真相
────────────────────────────────────────────
语义 在 [[HomeObject]] 的「当前原型」上查找
[[HomeObject]] 方法定义时挂在哪个对象上(如 B.prototype)
不是 静态绑定父类 ------ 断开 B.prototype 的原型后 super.m() 抛 TypeError
this 是调用者,不是 HomeObject
限制 只能在方法体内用 super.x,不能当值传递
instanceof 的算法
────────────────────────────────────────────
取 Ctor.prototype → 沿左侧对象的原型链找 → 命中 true / 到 null false
不看 constructor;Object.create(Ctor.prototype) 能骗过;可被 Symbol.hasInstance 接管
javascript
易错点
────────────────────────────────────────────
「class 就是语法糖」
→ 三处不是糖:不可枚举、强制严格、必须 new
「实例上有 prototype」
→ 没有。prototype 只挂函数上,给 new 当模板
「return null 会让 new 返回 null」
→ 不会。只有 return 对象才覆盖 this
「原型链终点是 Object.prototype」
→ 是 null。循环要以 p !== null 结束,否则漏掉 Object.prototype 自己那层
「super 就是调父类方法」
→ 它在 [[HomeObject]] 的当前原型上找。断开原型链后 super 会失效
「super 可以赋给变量再调用」
→ 不行。它是语法不是值,取出来就丢 this
「instanceof 能判断是不是 new 出来的」
→ 不能。只看原型链,Object.create(Ctor.prototype) 一样通过
「extends 只影响实例」
→ 还设了 Child.__proto__ = Parent,静态方法因此可继承
附:本文的数据来源与验证方法
| 数据项 | 来源 | 方法 |
|---|---|---|
| 四者身份关系(5 条) | Node 22.22.2 | === 比较 Foo.prototype.constructor / Object.getPrototypeOf(f) / f.__proto__ / Object.getPrototypeOf(Foo) 与各自期望值 |
new 的返回值规则 |
同上 | 四种构造函数分别 return 对象 / 原始值 / null / 不返回,检查实例上 a、b 是否存在 |
| 原型链四层与终点 | 同上 | 连续四次 Object.getPrototypeOf,逐层与该层原型做身份比较;第四层与 null 比较 |
| class 方法不可枚举 | 同上 | Object.keys(C.prototype) 与 Object.getOwnPropertyNames(C.prototype) 对照;与手写 Ctor.prototype.fn = ... 同测 |
| class 体内严格性 | 同上 | 把方法解构出来调用,检查 this === undefined;与普通函数直接调用得到 globalThis 对照 |
| class 不能无 new 调用 | 同上 | try { C() } catch 取 e.constructor.name |
| extends 两条链 | 同上 | Object.getPrototypeOf(Child.prototype) === Parent.prototype、Object.getPrototypeOf(Child) === Parent;并检查 hasOwnProperty(Child, 'kind') 为 false |
super 的 [[HomeObject]] 语义 |
同上 | 用 Object.setPrototypeOf 主动断开 Child.prototype 的原型,对比 super.m() 与 A.prototype.m.call(this) 两种写法的存活情况 |
super.m() 里的 this |
同上 | 在父类方法内读 this.tag,先在实例上打标记再调用 |
| instanceof 算法 | 同上 | 手写沿原型链查找的实现,与内置 instanceof 在正反两例上比对;Symbol.hasInstance 覆写后测 42 instanceof Weird |
| class 的 ES5 等价物(5 项) | 同上 | 用 Object.getOwnPropertyNames / Object.getOwnPropertyDescriptor 逐项比对;new.target 守卫对照抛错类型 |
| 规范依据 | ECMA-262 | OrdinaryCreateFromConstructor 与 [[Construct]] 的四步;ClassDefinitionEvaluation 里方法用 enumerable: false 定义;SuperProperty 通过 [[HomeObject]] 取 GetSuperBase();InstanceofOperator 与 @@hasInstance;extends 同时设置 protoParent 与 constructorParent |
所有断言在本机 Node 22.22.2 上复跑通过。涉及抛错的代码块显式标注 'use strict' ------ CommonJS 模块默认非严格 ,不标注会得到相反结论(frozen.a = 2 静默失败 vs 抛 TypeError)。涉及「结构等价」的结论一律以属性描述符为准,不以「看着一样」为准。文中引用的两处外部结论:《this 的四种绑定》讲「解构调用丢 this」,《Error.isError 与 using》讲「instanceof 跨 realm 失效」,两篇均在本库内,本文不重复其推导。