Allow IE to support function. bind ()

Source: Internet
Author: User

The front-end developer should be clear that the function objects of the Javscript can change the objects pointed to by the internal scope (this) through the call or apply method to achieve 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:

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:

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.