After some technical documents from google, I found that firefox supports a native bind method, which satisfies our original intention well. The call method is the same as the call and apply method, but after the definition is complete, this method will be executed only when it is called later. If you need a friend, you can understand that the front-end developer should be clear that the function object of the Javscript script can be called or applied, change the objects pointed to by the internal scope (this) to implement more scalable function development. Ie native supports the call and apply methods of function objects, which are also supported in firefox or other browsers. However, the call and apply methods work immediately and are executed. For example:
The Code is as follows:
Var func = function (){
Alert (this );
}. Apply (window );
When the script parsing engine executes this code, the dialog box is displayed immediately and the object string is displayed. Our original intention is to define the function of the func method on the window object domain and execute it later. However, the call and apply methods cannot meet our original intention, they will be executed immediately.
After some technical documents from google, I found that firefox supports a native bind method, which satisfies our original intention well. The call method is the same as the call and apply method, but after the definition is complete, this method is executed only when it is called later. However, this bind method is only supported by the ie10 browser, and an undefined error message is returned when executed in a browser earlier than this version. So I had to go online with the google solution again, but I had to pay close attention to it. We found a solution on the firefox development site, that is, adding the property prototype so that all browsers can support the bind method. The Code is as follows:
The Code is as follows:
If (! Function. prototype. bind ){
Function. prototype. bind = function (oThis ){
If (typeof this! = "Function "){
// Closest thing possible to the ECMAScript 5 internal IsCallable function
Throw new TypeError ("Function. prototype. bind-what is trying to be bound is not callable ");
}
Var events GS = Array. prototype. slice. call (arguments, 1 ),
FToBind = this,
FNOP = function (){},
FBound = function (){
Return fToBind. apply (this instanceof fNOP & oThis
? This
: OThis,
Using Gs. concat (Array. prototype. slice. call (arguments )));
};
FNOP. prototype = this. prototype;
FBound. prototype = new fNOP ();
Return fBound;
};
}
Understanding this code requires some knowledge. I just know how to use it. If anyone is interested, I would like to introduce the principle of this source code. Thank you very much!
It is simply not an attitude, but a satisfaction.