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.