原型链
new 与原型链
function _new(obj, ...rest){
// 基于obj的原型创建一个新的对象
const newObj = Object.create(obj.prototype);
// 将newObj作为函数内部的this上下文,并将rest数组中的元素作为参数传递给这个函数, 并获取obj函数执行的结果
const result = obj.apply(newObj, rest);
// 如果执行结果有返回值并且是一个对象, 返回执行的结果, 否则, 返回新创建的对象
return typeof result === 'object' ? result : newObj;
}
this 与原型链
Last updated