In javascript, you can use the Keri function to implement the bind method.

Source: Internet
Author: User

In javascript, you can use the Keri function to implement the bind method.

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;

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

The above is the code for implementing the bind method using the Keri function, hoping to help you learn javascript programming.

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.