es6 class 实例化本质是 es5 构造函数的语法糖,依赖原型链和 this 绑定;必须用 new 调用,否则报错;new 执行四步:创建空对象、链接 __proto__、绑定 this 执行 constructor、处理返回值;constructor 是唯一自动触发的方法;实例方法统一挂载在 prototype 上共享。

ES6 Class 的实例化过程本质上和 ES5 构造函数一致,只是语法更清晰。它不是全新机制,而是对 new Function() 流程的封装,底层仍依赖原型链和 this 绑定。
实例化必须用 new 调用
Class 不能像普通函数那样直接调用,否则会报错:
class Person { constructor(name) { this.name = name; } }Person('Alice') // TypeError: Class constructor Person cannot be invoked without 'new'- 必须写成
new Person('Alice')
new 执行时的四步核心流程
当你执行 new Person('Alice'),JavaScript 引擎内部按顺序完成以下操作:
- 创建一个空对象,作为即将生成的实例
- 将该对象的
__proto__指向Person.prototype - 以这个新对象为
this,执行Person内部的constructor方法 - 若
constructor显式返回一个对象,则返回该对象;否则默认返回新创建的实例
constructor 是关键入口
构造函数是实例化时唯一自动触发的方法:
- 不写
constructor,JS 会提供一个空的默认构造函数 - 写了就必须用
this初始化实例属性,比如this.name = name - 如果在
constructor中return { x: 1 },那实例就不再是Person类型,而是普通对象
方法都挂在 prototype 上
你在 class 里定义的实例方法(如 sayHello() {})不会复制到每个实例上,而是统一存在 Person.prototype 中:
Person.prototype.sayHello === p1.sayHello // true- 所以所有实例共享同一份方法,节省内存
- 这也意味着你可以后期通过
Person.prototype.walk = function(){}动态添加方法











