new 关键字
new 做了什么
- 创建一个对象
- 设置
__proto__
属性 - 设置
this
指向这个对象 - 执行构造函数
- 返回对象
function New(func) {
var res = {};
if (func.prototype !== null) {
res.__proto__ = func.prototype;
}
var ret = func.apply(res, Array.prototype.slice.call(arguments, 1));
if ((typeof ret === "object" || typeof ret === "function") && ret !== null) {
return ret;
}
return res;
}