Home > Web Front-end > JS Tutorial > body text

Do you know what the currying principle is? JavaScript function currying principles and usage examples

零下一度
Release: 2017-04-21 09:58:43
Original
1473 people have browsed it

This article mainly introduces the principle and usage of JavaScript function currying, analyzes the concept, principle, and function of function currying, and provides specific usage skills of currying functions in the form of examples. Friends who need it can Refer to

The examples in this article describe the principles and usage of Javascript function currying. Share it with everyone for your reference, the details are as follows:

Currying is a conversion process that transforms a function that accepts multiple parameters into one that accepts a single parameter (Annotation: the first parameter of the original function) The function, if additional arguments are necessary, returns a new function that accepts the remaining arguments and returns the result.

That is to say, it fixes some parameters and returns a function that accepts the remaining parameters, also called a partial calculation function. The purpose is to narrow the scope of application and create a more targeted function.

For example, I want to create a function to introduce myself. Everyone only needs to enter their name, gender, and age. But when A uses this function, he must enter his name and gender every time he calls it. In fact, only his age has changed. For this reason, A will generate a curried self-introduction function for himself, in which the name and gender are fixed. parameter.

Please check github for related codes.


function curry(fn){
  var args = Array.prototype.slice.call(arguments, 1);
  return function(){
    var innerArgs = Array.prototype.slice.call(arguments);
    var finalArgs = args.concat(innerArgs);
    return fn.apply(null, finalArgs);
  };
}
function selfIntroduction(name, gender, age){
  console.log('hi, I am ' + name + ', ' + age +' years old ' + '. I am a ' + gender + '.');
}
var curriedSelfIntroduction = curry(selfIntroduction, 'A', 'man');
curriedSelfIntroduction('12');
curriedSelfIntroduction('13');
curriedSelfIntroduction('14');
Copy after login

The results after execution.

Of course, we can also write a curried function that binds a new scope.


function curry(fn, context){
  var args = Array.prototype.slice.call(arguments, 2);
  return function(){
    var innerArgs = Array.prototype.slice.call(arguments);
    var finalArgs = args.concat(innerArgs);
    return fn.apply(context, finalArgs);
  };
}
Copy after login

Although currying functions is awesome, it also requires you to pay some attention to the order of parameters of the function you define. Function currying allows and encourages you to separate complex functionality into smaller and easier to analyze parts. These small logical units are obviously easier to understand and test, and then your application becomes a clean and tidy combination of small units. So if you can use curried functions appropriately, it will make your JS code more elegant.

Note: It is recommended that you take a look at Proxy in ES6 (ES2015), which is another way to preprocess functions.

The above is the detailed content of Do you know what the currying principle is? JavaScript function currying principles and usage examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!