Ke physical and chemical function thought: A JS pre-processing idea; using function execution can form a principle of not destroying scopes, storing what needs to be processed in this not-destroyed scope, and returning a small function, in which we perform small functions, In the small function of the previously stored value of the relevant operations can be processed;
The function of Gerty is mainly pretreatment;
The function of the bind method: To process the This in the callback callback method that is passed in as context contexts;
/**
* Bind method Implementation principle 1
* @param callback [function] callback function
* @param context [Object] contexts
* @returns {Function} changes This is the function to point to
/function bind (callback,context) {
var outerarg = Array.prototype.slice.call (arguments,2); /represents an argument that takes a parameter in the current scope except Fn,context, and the return
function () {
var innerarg = Array.prototype.slice.call (arguments , 0);//represents taking all arguments parameters in the current scope;
callback.apply (Context,outerarg.concat (Innerarg));
}
/**
* Imitate the principle of bind on the prototype chain (the thought of Cauchy function)
* @param context [Object] contexts
* @returns {function} to change this point
Function.prototype.mybind = Function Mybind (context) {
var _this = this;
var outarg = Array.prototype.slice.call (arguments,1);
Compatible with
if (' bind ' in Function.prototype) {return
this.bind.apply (This,[context].concat (Outarg));
}
In the case of incompatibility return
function () {
var inarg = Array.prototype.slice.call (arguments,0);
Inarg.length = = 0?inarg[inarg.length]=window.event:null;
var arg = Outarg.concat (inarg);
_this.apply (Context,arg);
}
Above this javascript use the Gerty function to implement the Bind method "Recommendation" is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.