bind 這里,可以不用fNOP函數嗎?
稍微修改了代碼試了一下,結果也是一樣的,請問為什么polyfill寫法要fNOP呢?
Function.prototype.myBind?=?function(_that){??var?fn?=?this;?//?fn?=?foo??var?_arguments?=?Array.prototype.slice.call(arguments).slice(1)??var?fNOP?=?function(){},??????fBound?=?function(){????????console.log(this)?????????var?that?=?this?instanceof?fn?&&?_that???this?:?_that;?????????var?args?=?Array.prototype.slice.call(arguments)????????var?bindArgs?=?_arguments.concat(args)????????return?fn.apply(that,?bindArgs)??????};??fBound.prototype?=?new?fn();?//?fBound繼承了fNOP的原型,也就是fn的原型,去修正返回的fBound函數的prototype對象??return?fBound;}function?foo(){??this.b?=?100;??return?this.a}var?func?=?foo.bind({a:1})func()new?func()
2021-07-12
Function.prototype.myBind?=?function(_that){ ??var?fn?=?this;?//?fn?=?foo?? ??var?_arguments?=?Array.prototype.slice.call(arguments).slice(1) ??var?fBound?=?function(){ ??????console.log(this)????????? ??????var?that?=?this?instanceof?fn?&&?_that???this?:?_that; ??????var?args?=?Array.prototype.slice.call(arguments)???????? ??????var?bindArgs?=?_arguments.concat(args)???????? ??????return?fn.apply(that,?bindArgs)?????? ????};?? ??????fBound.prototype?=?new?fn();?//?fBound繼承了fNOP的原型,也就是fn的原型,去修正返回的fBound函數的prototype對象?? ??????return?fBound; ????} ???? ????function?foo(){?? ??????this.b?=?100;?? ??????return?this.a ????} ????var?func?=?foo.bind({a:1}) ????func()? ????new?func()上面提問的代碼格式錯亂了,下面這里是正確排版,沒有用fNOP也可以得到一樣的效果,為什么需要用fNOP函數呢?