js 手动实现call方法
Function.prototype.myCall=function(content,...args){ let myfn = Symbol() content = content|| globalThis // console.log(content) content[myfn] = this // console.log(content) const result = content[myfn](...args) delete content[myfn] // console.log(content) return result } function myFn(a,b){ console.log(this.name,a,b) return 1 } console.log(myFn.myCall({name:'zhang'},3,4))
js 手动实现apply方法
Function.prototype.myApply = function(myThis,args){ myThis = myThis || globalThis; args = args || [] const fnName = Symbol() myThis[fnName] = this const result = myThis[fnName](...args) delete myThis[fnName] return result } function myFn(a,b){ console.log(this.name,a,b) return 1 } console.log(myFn.myApply({name:'zhang'},[3,4]))
js 手动实现bind方法
Function.prototype.myBind = function(myThis,...args1){ const myfn = this return function(...args2){ const result = myfn.apply(myThis,[...args1,...args2]) console.log(myfn,myThis) return result; } } function myFn(...args){ console.log(this.name,args) return 1 } let a = myFn.myBind({name:'zhang'},3,4) console.log(a(5,6)) console.log(a(5,6,7,8,9)) let b = myFn.myBind({name:'zhang'},3,4) console.log(b(5,6)) console.log(b(5,6,7,8,9))
标签:...,args,console,log,bind,javascript,content,call,myThis From: https://www.cnblogs.com/tongchuanxing/p/17816728.html