"Front-end learning notes" function curry (since NetEase cloud classroom)

Source: Internet
Author: User

1. The function of curry often refers to a technique that transforms a function that accepts multiple parameters into a new function that takes a single parameter (the first parameter of the original function) and returns an argument that accepts the remaining parameters and returns the result.

1. The simplest currying//SUM function accepts three parameters and returns the sum result var = function (a,b,c) {     return a+b+c;}//simplest curry sum function var sum_curry = function (a) {     return function (b,c) {         return a+b+c;     }}

2. A more generalized definition is to pass parameters to the function in step, each time the function accepts some parameters, and returns a function to accept the remaining parameters, which can be nested in a multi-layered function that accepts partial arguments until the final result is returned. Induction is the gradual transfer of parameters, gradually reduce the scope of the function, the process of gradually solving.

2. The generalization of the currying//currying implementation transforms a function into the curry function var currying = function (fn) {    var _args = [];    return function () {     if (arguments.length = = = 0) {       //implements the final computed       return fn.apply (this, _args);      }     Here is simply a cache of parameters (used to explain the concept of curry, not the actual application scenario)     Array.prototype.push.apply (_args, [].slice.call (arguments));      return Arguments.callee;    } }; The SUM function takes any arguments and returns the sum result var sum=function () {    var total = 0;    for (var i = 0, c; c = arguments[i++];) {Total        + = c;    }    return total; }; Or get a generalization of the sum function of the curry var sum_curry = currying (sum);  Sum_curry (1) (2,3); Sum_curry (4); Console.log (Sum_curry ());

3. From a higher level of understanding, Curry allows and encourages you to divide a complex process into smaller, easier-to-analyze processes (these small logical units will be easier to understand and test), and finally a complex process that is difficult to understand will become a combination of small logical simple processes.

3. The above two examples are a good explanation of what is the function of the curry, but when? Anything that simplifies the logic implementation and improves readability is encouraged, with a slightly more specific example://The Refresh function implements updating the data of the relevant module on the page via an AJAX request.    function Refresh (URL, callback) {///Ajax_get implements an AJAX get request, and the callback callback function after the request succeeds. Ajax_get (URL, callback); } The function update (data) {//updated logic is all handled here} refresh ("Xxx?target=news", update); The update function is a post-curry function, and the first-level function splits the modules that need to be updated according to the incoming parameters. function Update (target) {var _elm = document.getElementById (target);//The module is split, the code is just an example of return function (data) {///return a The Request Results Update page shows the function _elm.innerhtml = data; Here the implementation of the AJAX request returned Results Update page display process, the code is only an example}}//Update page can be written like this refresh ("Xxx?target=news", Update ("News")); Refresh ("Xxx?target=pictures", Update ("Pictures")); Continue, if the news module needs to continue to split into "social" news, "entertainment" news, then how do we curry the update function to write? It can be written like this: function update (target) {var _elm = document.getElementById (target);//The first-level module split is implemented here, the code is just an example of return function ( Type) {//Returns a function that accepts the remaining arguments var _elm = document.getElementById (item);//The second-level module is split here, the code is just an example of the return function (data ) {//Returns a function that accepts the remaining parameters and eventually updates the page display _elm.datA = data; Here the implementation of the AJAX request returned results update the page display process, the code is only an example}}}//Update page can be written like this refresh ("Action.do?target=news&type=society", update ("News") ("society")); Refresh ("Action.do?target=news&type=entertainment", Update ("News") ("entertainment"));

The front-end learning notes function curry (since NetEase cloud classroom)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.