美文网首页
new关键字过程分析

new关键字过程分析

作者: tenro | 来源:发表于2022-07-22 15:56 被阅读0次
function _new(Fn, ...args) {
  // 1、 创建一个空对象
  const obj = {};

  // 2、执行构造函数并将 this 指向刚刚创建的对象
  const result = Fn.call(obj, ...args);
 
  // 3.、修改新对象的原型对象
  obj.__proto__ = Fn.prototype;
 
  // 4、返回新对象
  return result instanceof Object ? result : obj;
}

相关文章

网友评论

      本文标题:new关键字过程分析

      本文链接:https://www.haomeiwen.com/subject/chzeirtx.html