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