Foreword
To understand hitch and partial functions, the premise is to understand The this variable in the function. In a javascript function, the this variable is actually an attribute of the Execution Context. Which object does this function belong to? Attribute, then this points to the object to which the function belongs; for example: global function and anonymous function this points to the global object window (in the browser environment); someObj.someFunc(); this points in someFunc() function The object is someObj.
But this is the general case, in fact, any object can be used as the this object in the function , You can change the direction of this variable through the functions Function.prototype.call and Function.prototype.apply; the functions implemented by call and apply are the same, but the parameter form is different, you can Baidu for details. In many js frameworks, I like to name this variable as scope (scope), but my personal understanding is not very appropriate. Scope and this in js are completely different things, but it is good to understand this variable. The name is just a code name.
The dojo.hitch and dojo.partial functions are implemented through the apply function, and their essence is Change the point of this variable (pointer) in the function, both functions will return a function again,The returned function this The variable is replaced by the first parameter.
dojo.hitch
The source code of the dojo.hitch function is as follows:
dojo._hitchArgs = function(scope, method /*,...*/){ //Exclude the first and second parameters and turn them into an array. In fact, the parameters after the method are used as the target function Parameters, converted into an array var pre = d._toArray(arguments, 2); //whether the method is a string type var named = d.isString(method); //return a new function return function(){ //will The arguments of this new function are converted into an array. Note that the upper and lower arguments are different. The previous one belongs to the dojo_hitchArgs function, and the next one is the new function returned by the attribute var args = d._toArray(arguments); // Take out the objective function. If the scope does not exist, the function is window[method] var f = named? (Scope||d.global)[method]: method; //Connect the two parameter arrays to form a complete parameter array, This is why the parameters can be passed in two passes through the hitch function //The return value of this new function is the return value of the target function execution, but the this variable is replaced return f && f.apply(scope || this, pre .concat(args)); // mixed }; // Function); dojo.hitch = function(/*Object*/scope, /*Function|String*/method /*,...*/){ // Scope is the object used to replace the this variable; method can be a string or a function to indicate which function to replace this Variable if(arguments.length> 2){ //If the number of parameters is more than two, call the dojo._hitchArgs function return d._hitchArgs.apply(d, arguments); // Function} //If the method is not specified if (!method){ method = scope; scope = null;} //If the method is a string type if(d.isString(method)){ //If the scope does not exist, the scope is a global object, that is, window scope = scope || d.global; if(!scope[method]){ throw(['dojo.hitch: scope["', method,'"] is null (scope="', scope,'")'].join (''));} //Return a new function that calls the scope[method] function and makes this point to the scope return function(){ return scope[method].apply(scope, arguments || []); }; // Function} //If the scope does not exist, the function is returned directly, and the this variable is not modified at this time //If the scope exists, the function is called directly, and this points to the scope return !scope? Method: function (){ return method.apply(scope, arguments || []); }; // Function};
Through the above analysis, we can know that the hitch function can have the following call forms:
1. The method is a string and no additional parameters are passed:
a. scope does not exist, scope is window object< br> b. If scope exists, the scope remains unchanged
The function returned by calls the scope[method] function, this points to the scope object;
2. The method is a string and additional parameters are passed:
At this time, connect the parameters on the basis of form 1.
3. The method is a function and no additional parameters are passed:
a. If the scope does not exist, the function will be returned directly, and the this variable is not modified at this time.
b . Scope exists, return a new function, the function calls method, this points to the scope object
4. Method is a function and passes additional parameters
此时,在形式3的基础上对参数进行连接
dojo.partial
The source code is as follows :
dojo.partial = function(/*Function|String*/method /*, ...*/){ var arr = [null ]; //Set scope to null return d.hitch.apply(d, arr.concat(d._toArray(arguments))); // Function};
As you can see, partial is implemented based on the hitch function, and it is very similar, except that compared to the hitch function, partial sets the scope to null. , So
dojo.partial(method) is equivalent to dojo.hitch(null, method). The difference between partial and hitch is that when passing parameters to the function, the hitch function can be divided into two parameters at most, while partial can be divided into multiple times. The reason is that the scope is empty when the partial function is called. For details, please refer to the calling form of the hitch function. Here is a specific example:
function bar(a, b, c) {console.info( "a=" + a + ",b=" + b + ",c=" + c);}var fn = dojo.hitch(window, "bar", 1, 2);fn(3); // a=1, b=2, c=3. Parameters can only be passed in two times fn = dojo.partial(bar, 1); fn = dojo.partial(fn, 2); fn(3); //a=1, b=2, c=3. Parameters can be passed in multiple times fn = dojo.partial(fn, 3); fn(); //a=1, b=2, c=3. Parameters can be passed in multiple times
PS:dojo version is dojo-release-1.6.3