js学习总结----柯里化函数
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                js学习总结----柯里化函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                柯里化函數函數思想:一個JS預處理的思想->利用函數執行可以形成一個不銷毀的私有作用域的原理,把需要預先處理的內容都存在這個不銷毀的作用域中,并且返回一個小函數,以后我們執行的都是小函數,在小函數中把之前預先存儲的值進行一系列的操作。
下面是模擬函數的bind方法的myBind,具體代碼如下
Function.prototype.myBind = function myBind(context){//this->fnvar _this = this;var outerArg = Array.prototype.slice.call(arguments,1);//兼容if("bind" in Function.prototype){return this.bind.apply(this,[context].concat(outerArg));}//不兼容return function(){var innerArg = Array.prototype.slice.call(arguments,0);innerArg.length===0?innerArg[innerArg.length] = window.event:null;//解決ie里面不傳遞e,是傳遞window.eventvar arg = outerArg.concat(innerArg);_this.apply(context,arg)} } //把傳遞進來的callback這個方法中的this預先處理為context(模擬函數的bind方法,兼容ie6-8) function bind(callback,context){context = context || window;var outerArg = Array.prototype.slice.call(arguments,2);//存儲的參數return function(){var innerArg = Array.prototype.slice.call(arguments,0);//存儲的e callback.apply(context,outerArg.cancat(innerArg));} }var obj = {name:"zhangsan"}; function fn(num1,num2){console.log(this,num1,num2); }window.setTimeout(fn.myBind(obj,100,200),1000);//這里把fn里面的this變成了obj?
轉載于:https://www.cnblogs.com/diasa-fly/p/7300626.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的js学习总结----柯里化函数的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: C#整数数据类型
 - 下一篇: imx8 uuu烧录