myNew
1 2 3 4 5 6
| function myNew(constructor, ...args) { let obj = {} obj.__proto__ = constructor.Protorype constructor.call(obj, ...args) return obj }
|
myBind
1 2 3 4 5 6
| Function.prototype.myBind = function(obj=window, ...outerArgs){ let _this = this return function(...innerArgs){ _this.call(obj, ...outerArgs.concat(innerArgs)) } }
|
myCall
1 2 3 4 5 6 7
| Function.prototype.myCall = function(obj=window, ...args){ obj.f = this const res = obj.f(...args) delete obj.f return res }
|
myApply
1 2 3 4 5 6
| Function.prototype.myApply = function(obj=window, args){ obj.f = this const res = obj.f(...args) delete obj.f return res }
|
ref
【前端面试】new, bind, call, apply 手写