function create(constructor, ...args) {
// 1.创建新对象
const target = {};
// 2.该对象的原型对象指向构造函数的原型
Object.setPrototypeOf(target, constructor.prototype);
// 3.构造函数的this指向该对象
const result = constructor.apply(target, args);
// 如果构造函数返回值为对象或函数则返回该对象或函数
if (typeof result === "object" || typeof result === "function") {
return result;
}
// 4.返回该对象
return target;
}
手写实现js中的new操作符
最新推荐文章于 2022-09-06 11:03:34 发布