new的作用
新⽣成了⼀个对象
链接到构造函数的原型
绑定
this
返回新对象
function new() {
let obj = new Object();
let Con = [].shift.call(arguments);
obj.__proto__ = Con.prototype
let result = Con.apply(obj,arguments);
return typeof result === 'object' ? result : obj;
}
instanceof的作用
instanceof可以正确的判断对象的类型
function instanceof(left, right) {
let prototype = right.prototype
left = left.__proto__
while(true) {
if(left === null)
return false
if(prototype === left)
return true
left = left.__proto__
}
}