Today, I came into contact with something I haven’t heard of before, and I feel very fun ~ to share with you ~ In order to thoroughly understand the relevant concepts, I specially read the link of zhang Xinxu’s related articles as follows ~ :
http://www.zhangxinxu.com/wordpress/?p=3048
Thank you for your sharing. This article will capture the part of your sharing and add your own little understanding.
In computer science, Currying is a technique that converts a function that takes multiple arguments into a function that takes a single argument (the first argument of the original function), and returns a new function that takes the remaining arguments and returns a result. The technique was named after logician Haskell Curry by Christopher Strachey, although it was invented by Moses Schnfinkel and Gottlob Frege.
“Currification” is just like a trick of some officials. Officials want to get 7 wives. Due to the national policy (monogamy) and the senility of the elder brother, there is only 1 wife on the surface, but the remaining 6 wives are actually digested in secret.
var curring = functionVar args = [].slice.call(arguments,1); // The first of the arguments arguments arguments generates an array, that is, the visible legal wives in the current contextreturn functionVar newArgs = args. Concat ([].slice.call(arguments)); // bind all parameters newArgs to fn~returnfn.apply(null,newArgs); }} // how can I get a real wife?function(){
var allWife = [].slice.call(arguments);
console.log(allWife.join('; '))},"Legitimate wife."); // Get the other 6 wives"The 1st Wife"."The 2nd Wife"."The 3rd Wife"."The 4th Wife"."The 5th Wife"."The 6th Wife"); // Get a new wife"The first wife"."The 4th wife"."Pretty Wife"."Rogue Wife."."Good wife"."Give me your wife."); // Get a new wife'More than Trinket's wife');
Copy the code
The effect is this
Fn. Apply (null,newArgs) {fn. Apply (null,newArgs) {fn. Apply (null,newArgs)
There are three common applications of Currification:
- Parameter reuse (legitimate wife in code above)
- Here is a very useful example, compatible with modern browsers and IE browser event add method:
var addEvent = function(el, type, fn, capture) {
if (window.addEventListener) {
el.addEventListener(type.function(e) {
fn.call(el, e);
}, capture);
} else if (window.attachEvent) {
el.attachEvent("on" + type.function(e) { fn.call(el, e); }); }};Copy the code
What’s wrong with the above method? Obviously, every time we use addEvent to add an event to an element, (eg. IE6/IE7) will pass through if… else if … In fact, you only need to decide once. How do you do that? — Corrification. Change to code like this:
var addEvent = (function() {if (window.addEventListener) {
return function(el, sType, fn, capture) {
el.addEventListener(sType, function(e) {
fn.call(el, e);
}, (capture));
};
} else if (window.attachEvent) {
return function(el, sType, fn, capture) {
el.attachEvent("on" + sType, function(e) {
fn.call(el, e);
});
};
}
})();
Copy the code
The initial implementation of addEvent actually implements only part of the application (only once if… else if… Decision), and the rest of the argument application is its return function implementation, a typical Currie.
- Third common application: delay computing
var curryWeight = function(fn) {
var _weight = [];
return function() {
if (arguments.length === 0) {
return fn.apply(null, _weight);
} else{ _weight = _weight.concat([].slice.call(arguments)); }}}; var weight = 0; var addWeight = curryWeight(function() {
var i=0; len = arguments.length;
for(i; i<len; i+=1) { weight += arguments[i]; }}); AddWeight (2.3); AddWeight (6.5); AddWeight (1.2); AddWeight (2.5); addWeight(); // Console. log(weight); / / 12.5Copy the code
The argument is passed in first, and the calculation ~ is not performed until it is called
Today’s share is here ~ is willing to share with you ~