Some of these things have been discussed several years ago. It doesn't matter if I write them to myself.
Http://ejohn.org/apps/learn/#2
// The .bind method from Prototype.js Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift(); return function(){ return fn.apply(object, args.concat(Array.prototype.slice.call(arguments))); }; };
The following two functions can help you better understand this function.
1. http://www.mymickey.org /? P = 97
Keep a backup
Function. prototype. bind method in prototype. js
The first time you see prototype. functions in the js framework. prototype. bind = function () {} and class. the create () method does not really understand it, because mickey usually does not think much about the prototype methods provided for us in these powerful libraries and their ease of use when using jquery. So after seeing some of the source code of prototype. js frameworks, it is difficult to understand the clever use of some basic class extension prototype methods. However, the mickey is analyzed layer by layer and the segments are called. Therefore, the following conclusions are drawn. If there are any deficiencies, please leave a message to correct them.
Function. prototype. bind method in prototype. js:
123456789 |
Function.prototype.bind = function() { var __method = this; var args = Array.prototype.slice.call(arguments); var object=args.shift(); return function() { return __method.apply(object, args.concat(Array.prototype.slice.call(arguments)));}} |
Add a new prototype (prototype) method bind to all function objects:
- Save the object that calls the bind method to the _ method variable.
- Convert the parameters passed when the bind method is called into an array and save it to The args variable.
- Extract the first [0] element of The args array and save it to the variable object.
- Returns a function.
When the returned function is called again, perform the following operations:
- Use the apply method to replace the this pointer in the function that calls the bind method with the object.
- Convert the parameters passed to this anonymous function into an array, and combine them with the args array to form a new array and pass it to the _ method.
Var args = Array. prototype. slice. call (arguments) can be abbreviated as this []. slice. call (arguments); but this will add an empty array object to the memory. In order to simplify the reading point, you can fake it as a form of slice. call (arguments). If you remove the call, the function slice (arguments ){....}, In fact, slice is a prototype method defined for all arrays. What is different from our custom prototype method is that javascript is a preset built-in method for developers, it can only be called by arrays. In the editor, what mickey obtains after alert (Array. prototype. slice) is funciton slice (){...}.
Since slice is just a common function method, slice. call (object) can also temporarily replace this in slice. Just as the this pointer in []. slice () {...} points to an array object that calls the slice method. Using the call method or the apply method, you can temporarily change the this pointer in the object that calls this method to the first parameter in the passing parameter, which is temporary, it is reset after the memory is released after the function is executed.
Var object = args. shift () saves the object at the [0] position in the array to the object variable. This object is the pointer to be replaced.
123456 |
function o(){ this.num = 1; var fn = function(arg){alert(this.num+arg)}.bind(this,2); fn();}new o() |
Line 2 of the Code: An anonymous function calls the bind method and passes the o object and a number 2 to the bind function. The two parameters passed in the past will be converted into arrays, the o object is extracted separately and put into the object. After the bind function is called, a function is returned and saved to the fn variable. Now, the bind function is saved to the fn variable, the content in fn is the fifth to seventh lines of the first code block of the article.
Line 2 of the Code: Call the fn function, or call the return value of the bind function. "3" is displayed ".
The anonymous function in the bind method indirectly stores the variables in its runtime environment. This method is usually called a closure ".
This is the powerful method and attribute developed by many open-source frameworks for developers. For beginners of mickey, if the above remarks are incorrect, I still want to reluctantly forgive them, if you find anything inappropriate, please leave a message.
2. http://stackoverflow.com/questions/1854622/prototype-bind-method-issue