function create(obj){
function F(){}
F.prototype = obj;
return new F()
}
function create(obj){
function F(){}
F.prototype = obj;
return new F()
}
function create(obj){
function F(){}
F.prototype = obj;
return new F()
}
function create(obj){
function F(){}
F.prototype = obj;
return new F()
}
2. 手写 instanceof 方法
function myInstanceof(left, right){
let proto = Object.getPrototypeOf(left);
prototype = right.prototype;
while(true){
if(!proto) return false;
if(proto === prototype) return true;
proto = Object.getPrototypeOf(proto);
}
}
function myInstanceof(left, right){
let proto = Object.getPrototypeOf(left);
prototype = right.prototype;
while(true){
if(!proto) return false;
if(proto === prototype) return true;
proto = Object.getPrototypeOf(proto);
}
}
function myInstanceOf(left, right){
let proto = Object.getPrototypeOf(left);
prototype = right.prototype;
while(true){
if(!proto) return false;
if(proto === prototype) return true;
proto = Object.getProtortpeof(proto)
}
}
function myInstanceOf(left, right){
let proto = Object.getPrototypeOf(left)
prototype = right.prototype;
while(true){
if(!proto) return false;
if(proto == prototype) return true;
proto = Object.getPrototypeOf(proto)
}
}
3. 手写 new 操作符
function myNew(constructor, ...args){
if(typeof constructor !== "function") return;
let obj = {}
obj.prototype = Object.create(constructor.prototype)
const res = constructor.apply(obj, args)
if(res && (typeof res !== "object" || typeof res === "function")) return res;
return obj;
}
function Fn(obj){
this.obj = obj;
}
let obj = myNew(Fn, "222")
console.log(obj);
function myNew(constructor, ...args){
if(typeof constructor !== "function") return;
let obj = {}
obj.prototype = Object.create(constructor.prototype)
const res = constructor.apply(obj, args)
if(res && (typeof res !== "object" || typeof res === "function")) return res;
return obj;
}
function Fn(obj){
this.obj = obj
}
let obj = myNew(Fn, "222")
console.log(obj)
function myNew(constructor, ...args){
if(typeof constructor !== "function") return;
let obj = {}
obj.prototype = Object.create(constructor.prototype)
const res = constructor.apply(obj, args)
if(res && (typeof res !== "object" || typeof res === "function")) return res;
return obj;
}
1. 手写 Object.create
2. 手写 instanceof 方法
3. 手写 new 操作符
4. 手写 Promise
5. 手写防抖函数
6. 手写节流函数
7. 手写类型判断函数
8. 手写 call 函数
9. 手写 apply 函数
10. 手写 bind 函数