实现 JavaScript 中的 new 关键字

众所周知 JavaScript 是一门基于原型、函数先行的多范式语言,它已经由ECMA(欧洲电脑制造商协会)通过ECMAScript实现语言的标准化。

想要知道如何实现 new 关键字我们就得搞清楚 new 做了什么。

JavaScript 中的 new 做了什么 ?

按照 spec ,new 依次做了以下 4 件事情:

  1. 创建一个对象 new Object
  2. 链接该对象(即设置该对象的构造函数)到另一个对象 ;
  3. 将步骤 1 新创建的对象作为 this 的上下文;
  4. 如果构造函数没有返回对象则返回 this

最终实现代码

1
2
3
4
5
6
function objFactory() {
var Constructor = [].shift.call(arguments)
var obj = Object.create(Constructor.prototype)
var ret = Constructor.apply(obj, arguments)
return (typeof ret instanceof Object || ret === null) ? ret : obj
}

设计模式 javascript-extends
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×