New内部原理:
function Say(name) {
this.name = name;
}
Say.prototype.back = function () {
console.log('back');
}
Say.prototype.something = function () {
console.log('something', this.name);
}
let newSay = new Say('haha');
// newSay.back();
// newSay.something();
// new内部
function _new(fn, ...params){
// let obj = {};
// obj.__proto = fn.prototype;
let obj = Object.create(fn.prototype);
res = fn.apply(obj, params);
if (res !== null &&( typeof res === 'object' || typeof res === 'function' )) {
return res;
} else {
return obj;
}
}
let new1 = _new(Say, 'test');
new1.back(); // back
new1.something(); // something test