Ke Lihua explanation of JavaScript Functions

Source: Internet
Author: User
Keri functions are mainly used for preprocessing. Next, I will introduce the functions of JavaScript function ke Lihua and the bind implementation method through this article. It is of great reference value and I would like to share it for you to learn. What is kerizhua?

Curialization is a conversion process that converts a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function, if other parameters are necessary, return the new function that accepts the remaining parameters and returns the result.

Ke's Thoughts on physical functions:A js pre-processing idea; using function execution can form a principle that does not destroy the scope, and store all the content to be pre-processed in this scope that is not destroyed, and return a small function. We will execute all the small functions in the future. In the small function, we can perform related operations on the previously stored values;

Keri functions are mainly used for preprocessing;

Bind method:Prefix this in the passed callback method as context;

The bind method implementation principle 1 code is as follows:

/*** Bind method implementation principle 1 * @ param callback [Function] callback Function * @ param context [Object] context * @ returns {function} change the Function pointed to by this */Function bind (callback, context) {var outerArg = Array. prototype. slice. call (arguments, 2); // indicates the parameters in the current scope except fn and context; return function () {var innerArg = Array. prototype. slice. call (arguments, 0); // indicates taking all the arguments parameters in the current scope; callback. apply (context, outerArg. concat (innerArg ));}}

The following code imitates the bind implementation principle on the prototype chain.

/*** Simulate the bind implementation principle on the prototype chain (KE Physical Function Idea) * @ param context [Object] context * @ returns {Function} changes the Function that this points to */Function. prototype. mybind = function mybind (context) {var _ this = this; var outArg = Array. prototype. slice. call (arguments, 1); // if ('bind' in Function with compatibility. prototype) {return this. bind. apply (this, [context]. concat (outArg);} // return function () {var inArg = Array. prototype. slice. call (arguments, 0); inAr G. length = 0? InArg [inArg. length] = window. event: null; var arg = outArg. concat (inArg); _ this. apply (context, arg );}}

Currying)

In computer science, colialization converts a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function, and return the technology of the new function that accepts the remaining parameters and returns the result.

Curialization is to pass in some parameters in advance to obtain a simple function. But the pre-passed parameters are stored in the closure, so there are some special features. For example:

Example:

Var adder = function (num) {return function (y) {return num + y;} var inc = adder (1); var dec = adder (-1 ); // inc, dec is now two new functions, which are used to pass in the parameter value (+/-) 1 alert (inc (99 )); // 100 alert (dec (101); // 100 alert (adder (100) (2); // 102 alert (adder (2) (100 )); // 102

The above is a detailed description of the JavaScript function ke Lihua. For more information, see the PHP Chinese website (www.php1.cn )!

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.