Using colized functions in javascript to implement the bind method [recommended] _ javascript tips-js tutorial

Source: Internet
Author: User
The following small make up will bring you an article on using the Keri function in javascript to implement the bind method [recommended ]. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at Ke's function thought: A js pre-processing idea; using function execution can form a principle of no-destruction scope, store all the content to be pre-processed in this non-destruction scope, and return a small function, in the future, we will execute all the small functions. In the small functions, we can perform operations on the previously stored values;

• Keri functions are mainly used for preprocessing;

• Bind method: preprocesses this in the passed callback method as context;

/*** 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 ));}}

/*** 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 (argument S, 0); inArg. length = 0? InArg [inArg. length] = window. event: null; var arg = outArg. concat (inArg); _ this. apply (context, arg );}}

In the above javascript article, the bind method implemented by using the Keri function [recommended] is all the content shared by the editor. I hope to give you a reference and support for your feet.

Related Article

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.