JavaScript Object Bind () method compatible processing

Source: Internet
Author: User

The bind () function was added to the ECMA-262 fifth version; it may not run on all browsers. You can make it work by adding the following code partially at the beginning of the script so that unsupported browsers can also use the bind () function

if(!Function.prototype.bind) {Function.prototype.bind=function(othis) {if(typeof  This!== "function") {      //closest thing possible to the ECMAScript 5      //Internal iscallable function      Throw NewTypeError ("Function.prototype.bind-what is trying to be bound are not callable"); }    varAargs = Array.prototype.slice.call (arguments, 1), Ftobind= This,//This here points to the target functionFnop =function() {}, Fbound=function() {          returnFtobind.apply ( This instanceofFnop? This //here This is the obj that is generated when you call new obj (): Othis | | This,//bind Fbound to this if Othis is invalid            //The arguments passed by BIND are merged with the arguments passed at the call and passed as the final argumentAargs.concat (Array.prototype.slice.call (arguments)));    }; //Copy the prototype object of the target function into the new function, because the target function may be used as a constructorFnop.prototype = This. prototype; Fbound.prototype=NewFnop (); returnFbound; };}

JavaScript Object Bind () method compatible processing

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.