




















function myNew(constructor,...args){ if(typeof constructor!=='function'){ return new TypeError('constructor must be a function'); } const newObj=Object.create(constructor.prototype); const instance=constructor.apply(newObj,args); return instance instanceof Object ? result : obj;
}
function myNew(Constructor, ...args) { // 1. 创建一个空对象 const obj = {}; // 2. 设置对象的原型为构造函数的原型 // 注意:__proto__ 不是标准属性,但现代浏览器都支持 obj.__proto__ = Constructor.prototype; // 3. 调用构造函数,绑定 this const result = Constructor.apply(obj, args); // 4. 返回结果 return result instanceof Object ? result : obj; }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。