pengzhanbo
176字小于1分钟
2022-04-22
提问
new
操作符 具体做了什么?new
?this
指向这个对象的原型,并执行构造函数的代码function newFactory(constructor, ...args) {
if (typeof constructor !== 'function') {
throw new Error('constructor must be a function')
}
const object = Object.create(constructor.prototype)
const result = constructor.apply(object, args)
if (result && (typeof result === 'object' || typeof result === 'function')) {
return result
} else {
return object
}
}