# 实现 new
# 实现步骤
- 创建一个全新的对象
- 这个对象会被执行
[[Prototype]](也就是__proto__链接) - 生成的新对象会绑定到函数调用的 this
- 通过 new 创建的每个对象将最终被
[[Prototype]]链接到这个函数的prototype对象上 - 如果函数没有返回对象类型
Object(包含Function、Array、Date、RegExp、Error),那么new表达式中的函数会自动返回这个新的对象
# 代码实现
function _new(Ctor) {
if (typeof Ctor !== 'function') {
throw new TypeError(Ctor + ' is not a constructor')
}
// es6
_new.target = Ctor
const obj = Object.create(Ctor.prototype) // 步骤 1,2,4
const args = [].slice(arguments, 1)
const result = Ctor.apply(obj, args) // 步骤 3
const isObject = result !== null && typeof result === 'object'
const isFunction = typeof result === 'function'
if (isObject || isFunction) { // 步骤 5
return result
}
return obj
}