What is Curricula
Currying is a conversion process that converts a function that accepts multiple parameters into a function that accepts a single parameter (translation note: the first parameter of the original function). If other parameters are necessary, return a new function that accepts the remaining parameters and returns the result.
Keli Chemical Function Thought: A js pre-processing idea; the principle that using function execution can form a scope that is not destroyed can be formed, and all the content that needs to be pre-processed is stored in this scope that is not destroyed can be returned, and a small function will be executed in the small function.
Currying function mainly plays a role in preprocessing;
The function of the bind method: preprocess this in the callback callback method passed in as a context context;
The code of the bind method implementation principle 1 is as follows:
/*** principle of implementation of bind method 1* @param callback [Function] Callback function* @param context [Object] Context* @returns {Function} Change the function pointed to by this*/function bind(callback,context) {var outerArg = Array.prototype.slice.call(arguments,2);// Indicates taking the parameters after fn and context in the current scope; return function (){var innerArg = Array.prototype.slice.call(arguments,0);// means to take all arguments parameters in the current scope; callback.apply(context,outerArg.concat(innerArg));}}The following code imitates the principle of bind implementation on the prototype chain
/*** Imitate the principle of bind implementation on the prototype chain (Korean and chemical function idea)* @param context [Object] Context* @returns {Function} Change the function pointed to by this*/Function.prototype.mybind = function mybind (context) {var _this = this;var outArg = Array.prototype.slice.call(arguments,1);// If('bind' in Function.prototype) {return this.bind.apply(this,[context].concat(outArg));}// Return function in case of incompatibility () {var inArg = Array.prototype.slice.call(arguments,0);inArg.length === 0?inArg[inArg.length]=window.event:null;var arg = outArg.concat(inArg);_this.apply(context,arg);}}Currying function
In computer science, Curry is a technique of converting a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returning a new function that accepts the remaining parameters and returns the result.
Currying means passing certain parameters in advance to get a simple function. But pre-passed parameters are saved in the closure, so there are some peculiar features. for example:
example:
var adder = function(num) {return function(y) {return num + y;}}var inc = adder(1);var dec = adder(-1);//inc, dec is now two new functions, which is used to pass in parameter value (+/-)1alert(inc(99));//100alert(dec(101));//100alert(adder(100)(2));//102alert(adder(2)(100));//102The above content is the curling JavaScript function and the method to implement bind methods introduced to you by the editor. I hope it will be helpful to everyone!