Learning notes for JavaScript function colialization

Source: Internet
Author: User

I. Ke Lihua function definition

Function currying, also known as partial evaluation, refers to the process of gradually passing parameters and solving them.

II. Implementation of function Keri

A Curry function first accepts some parameters. After the parameter is accepted, the function does not evaluate the value immediately, but returns another function, the parameters just passed in are saved in the closure formed by the function. When the function is actually required, all previously passed parameters will be used for value evaluation at one time.

As follows:

Var add = (function (){
Var numArr = [];

Return function (){
If (arguments. length = 0 ){
Var result = 0;
For (var I = 0, len = numArr. length; I <len; I ++ ){
Result + = numArr [I];
            }
Return result;
} Else {
// NumArr. push (arguments );
Array. prototype. push. apply (numArr, arguments );
        }
    }
})();

/* = Client call = */
Add (21 );
Add (2 );

Console. log (add (); // 23

III. General Keri functions

Ke Lihua's operations can be summarized:

Accepts a function;
Returns a function that receives only one parameter.
The common Keri functions can be as follows:

Var currying = function (fn ){
Var args = [];

Return function (){
If (arguments. length = 0 ){
// Perform real value calculation
Return fn. apply (this, args );
} Else {
// Save all the called parameters
Array. prototype. push. apply (args, arguments );
Return arguments. callee;
        }
    }
};

The add function can be rewritten as follows:

Var add = (function (){
Var result = 0;
Return function (){
For (var I = 0, len = arguments. length; I <len; I ++ ){
Result + = arguments [I];
        }
Return result;
    }
})();
The call result is as follows:

/* = Client call = */
Var addCurrying = currying (add); // convert to the currying function
AddCurrying (21 );
AddCurrying (2 );

Console. log (addCurrying (); // 23

IV. Anti-kerizalization

The opposite of anti-kerialization is to delay the transmission of fixed parameters or this context as parameters to the future.

The function of anti-Keri is applied to the extended functions, so that functions originally owned by a specific object can be used by any object. In a more general explanation, anti-Corrie refers to the borrow of a function, which can be used by a function to process other objects and extended the use scope of the function by borrowing generalization.

The anti-Keri statement is as follows:

4.1 Statement 1:

Var uncurrying = function (fn ){
Return function (){
Var context = []. shift. call (arguments );
Return fn. apply (context, arguments );
    }
};

4.2 Statement 2:

Var uncurrying = function (fn ){
Return function (){
Return Function. prototype. call. apply (fn, arguments );
    }
}

4.3 Statement 3:

Var uncurrying = Function. prototype. bind. bind (Function. prototype. call );
4.4 actual anti-Keri call example:

/* = Client call = */
Var push = uncurrying (Array. prototype. push); // Anti-curryization

Var arr = {};
Push (arr, ["Bob", "Jenny", "Tom"]);

Console. log (arr); // Object {0: "Bob", 1: "Jenny", 2: "Tom", length: 3}

5. Applicable scenarios of Keri

When the vast majority of parameters passed by a function are the same, and the result of the function can be postponed until the parameter is exhausted, this function is suitable for the use of Keri.

VI. Keri-based performance analysis

Ke Lihua certainly has some performance overhead. The specific performance analysis is as follows:

Accessing arguments objects is generally slower than accessing named parameters;
Some earlier versions of browsers are quite slow in implementing arguments. length;
Using fn. apply () and fn. call () is slower than directly calling fn;
Creating a large number of nested scopes and closures brings about overhead.
VII. Summary

Function kerialization allows and encourages you to separate complex functions into smaller and easier parts for analysis. These small logical units are obviously easier to understand and test, and then your application will become a clean and neat combination consisting of some small units.

However, function kerialization also brings about some performance overhead. Suitable for use in project development.

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.