JavaScript 中new到底做了什么?

261 阅读1分钟

首先看new的语法

  • new constructor[([arguments])] 其中,constructor 可以是一个class或者是function

在我们调用new运算符的时候,遵循以下几个步骤

  1. 创建一个空的对象 {}
  2. 给新创建的空对象的__proto__ 绑定属性,绑定为constructor.prototype
  3. 指定新创建的对象作为this的上下文
  4. 如果该函数没有返回对象,则返回this