JavaScript new 的实现

new 特性

《你不知道的JavaScript》提到:

使用 new 调用函数,或者说发生构造函数调用时,会自动执行下面的操作:

  • 创建(或者说)构造一个全新的对象;
  • 这个新对象会被执行 [[Prototype]] 连接;
  • 这个新对象会绑定到函数调用的 this;
  • 如果函数没有返回其他对象,那么 new 表达式中的函数调用会自动返回这个新对象。

new 的实现

用法: fakeNew(Constructor, …)

function fakeNew() {
  let obj = {};

  // 将参数中的构造函数与构造函数的参数分离开(shift方法修改原数组)
  let Constructor = Array.prototype.shift.call(arguments); 

  // 实例的隐式原型指向实例原型,即进行 [[Prototype]] 连接
  obj.__proto__ = Constructor.prototype; 

  // 通过 apply 使 this 指向要返回的实例 obj。即新对象被绑定到函数调用的 this。
  let result = Constructor.apply(obj, arguments); 

  // 在函数没有返回对象的情况下,返回这个新对象(注)
  return typeof result == "object" ? result || obj : obj;
}

注: result || obj 是因为当构造函数返回 null 时,应当返回新实例对象。

  • null 是空对象指针,即空对象的引用。
  • typeof null 会返回 object

检测

function Test(a, b) {
  this.a = a;
  this.b = b;
  // return {
  //   a: 1,
  //   b: 2
  // };
}
Test.prototype.c = 3;
Test.prototype.add = function() {
  return this.a + this.b;
};

let test = fakeNew(Test, 6, 9);

console.log(test);
console.log(test.c);
console.log(test.add());

Test { a: 6, b: 9 }
3
15

如果 Test 函数返回那个被注释的对象,则会输出:

{ a: 1, b: 2 }
undefined
… …
TypeError: test.add is not a function // 此时是 undefined

参考:https://github.com/mqyqingfeng/Blog/issues/13

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值