What is a function Coriolization?

Converts a function that takes multiple arguments to a function that takes a single argument, returns nested and uses all arguments, and returns the resulting value.

What’s the use of currization of a function? Or what good is it?

  • Parameters of reuse

The following example reuses regular expressions without passing in regular and STR arguments each time.

function myTest(reg) {
    return function(str) {
        return reg.test(str);
    }
}
var test = myTest(/\d/g);
var res1 = test('123');
var res2 = test('sadads');
var res3 = test('1d2ds3');
Copy the code
  • Delay calculation
Copy the code
  • Dynamically generated function
const addEvent = (function() { if (window.addEventListener) { return function(ele) { return function(type) { return function(fn) { return function(capture) { ele.addEventListener(type, (e) => fn.call(ele, e), capture); } } } } } else if (window.attachEvent) { return function(ele) { return function(type) { return function(fn) { return function(capture) { ele.addEventListener(type, (e) => fn.call(ele, e), capture); }}}}}})(); // Call addEvent(document.getelementById ('app'))('click')((e) => {console.log('click function has been called :', e); })(false); Const ele = document.getelementById ('app'); const ele = document.getelementByid ('app'); // get environment const environment = addEvent(ele) // bind event environment('click')((e) => {console.log(e)})(false);Copy the code

How do you do that? How do you convert a function to a Cremation?

function curry(fn) { return function nest(... If (fn. Length === args1. Length) {return fn(... args1); }else {return function(arg) {return nest(... args1, arg); } } } } function addNum(a, b, c) { return a + b + c; } const addCurry = curry(addNum); addCurry(1)(2)(3); / / 6Copy the code