Simulate native BIND () functions compatible with earlier versions of IE browsers

Source: Internet
Author: User

Simulate and implement native BIND () functions compatible with earlier versions of IE:
For more information about the basic functions of JavaScript BIND (), see section 1 about the usage of JavaScript BIND () functions.
However, this function is not supported in IE8 and IE8 browsers. The following describes how to simulate and implement this function.
The Code is as follows:

If (! Function. Prototype. Bind) {function. Prototype. Bind = function (othis) {If (typeof this! = 'Function') {Throw new typeerror ('caller is not the current function object');} var response GS = array. prototype. slice. call (arguments, 1), ftobind = This, fnop = function () {}, fbound = function () {return ftobind. apply (this instanceof fnop & othis? This: othis | window, memory Gs. concat (array. prototype. slice. call (arguments) ;}; fnop. prototype = This. prototype; fbound. prototype = new fnop (); Return fbound ;};}

The above code implements compatibility. The following describes the implementation process.
I. Code comments:
1. If (! Function. Prototype. Bind) to determine whether the current browser supports the BIND () function.
2. function. Prototype. Bind = function (othis) {}. If not, add a bind function. The parameter is the object to which this points.
3. If (typeof this! = 'Function '){
Throw new typeerror ('caller is not the current function object ');
}, Because the call or apply method is used, the caller can be converted to another object, such as func. Bind. Call (OBJ), to prevent this situation.
4. var parameters GS = array. Prototype. Slice. Call (arguments, 1), get the parameters passed by the BIND function starting from the second one.
5. ftobind = This: assign this reference of the current function object to the variable ftobind.
6. fnop = function () {}: Create a function using an expression. Its usage is described later.
7. fbound = function (){
Return ftobind. Apply (this instanceof fnop & othis? This: othis | window,
Using Gs. Concat (array. Prototype. Slice. Call (arguments )));
}, The fbound function is the function to be returned. Return is added before return ftobind. Apply () because the ftobind function may return values.
This instanceof fnop & othis? This: othis | window, this instanceof fnop can be used to determine whether it is used as a constructor. As for & whether it is worth discussing with the othis following the operator, if you follow the source code of MDN, when being used as a constructor without passing the othis parameter, the ftobind function will be called using the window object. If there is no othis, whether or not the BIND () function passes the othis parameter, when the fbound function is used as the constructor, you can use this to call the ftobind () function.
When Gs. Concat (array. Prototype. Slice. Call (arguments) is connected, the final result is the parameter to be passed to the ftobind () function.
8. fnop. Prototype = This. Prototype. This sentence is completely unnecessary, probably to increase the density of the prototype chain.
9. fbound. prototype = new fnop (): Set the prototype object of fbound to the Instance Object of fnop (). In this way, if fbound is used as the constructor, this instanceof fnop returns true.
10. Return fbound, return this function.
Ii. Related reading:
1. For the usage of the typeof operator, see section 1 of the usage of the typeof operator in JavaScript.
2. For details about the slice () method, refer to slice () method 1 of the array object in JavaScript.
3. For more information about the call () and apply () methods, see the functions and differences between call () and apply () in JavaScript.
4. For more information about the Concat () function, see the usage section of Concat () function in JS array.
5. For prototype prototypes, refer to the prototype overview section in JavaScript.

Simulate native BIND () functions compatible with earlier versions of IE browsers

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.