concept

Curry’s concept is simple: Call a function with just a few arguments and let it return a function that handles the rest. You can call the Curry function once or multiple times, passing one argument at a time.

So far, I have seen the best interpretation of Currification.

A simple example

var add = function(x) {
  return function(y) {
    return x + y;
  };
};

var increment = add(1);
var addTen = add(10);

increment(2);
/ / 3

addTen(2);
/ / 12
Copy the code

Application scenarios

1. The Vant component library simplifies BEM usage

Rate Scoring Component

const [createComponent, bem] = createNamespace('rate');
Copy the code

In this case, a component’s namespace is implemented by using the function corrification

export function createNamespace(name: string): CreateNamespaceReturn {
  name = 'van-' + name;
  return [createComponent(name), createBEM(name), createI18N(name)];
}
Copy the code
export function createBEM(name: string) { return function (el? : Mods, mods? : Mods): Mods { if (el && typeof el ! == 'string') { mods = el; el = ''; } el = el ? `${name}__${el}` : name; return `${el}${gen(el, mods)}`; }; }Copy the code

2. Vue2. X source code application

The browser, server, and other host environment wrapped, return a function containing the runtime environment. See the Runtime directory section of the vue source code for details.

reference

Chapter 4: Curry