function _new(fn, ...args) {
if(typeof fn !== 'function') {
throw Error('fn 必须是一个函数')
}
const p = Object.create(fn.prototype);
const res = fn.call(p, ...args);
if(res !== null && (typeof res === 'object')) {
return res;
}
return p;
}
function B() {
this.name = 'mike';
}
B.prototype.sayName = function() {
console.log(this.name);
}
console.log(new B())
console.log(_new(B))
function _instanceof(target, source) {
let t = Object.getPrototypeOf(target);
while(t){
if(t === source.prototype){
return true;
}
t = Object.getPrototypeOf(t);
}
return false;
}
console.log(_instanceof(_new(B), B))
function C() {
B.call(this);
this.age = 12;
}
C.prototype = Object.create(B.prototype)
C.constructor = C;
console.log(new C())
console.log(_instanceof(_new(C), B))
console.log(_instanceof(Number(1), Number))
console.log(_instanceof([], Array))
console