Introduction to the use of JavaScript bind _javascript Tips

Source: Internet
Author: User
A few days ago to see a face test, the topic is this:
What do you say about Apply,call,bind in JavaScript?
It's a cliché to apply and call first, but for bind, I froze because the word was a very high frequency in jquery used to bind events to DOM elements.
To make sense of this unfamiliar and familiar bind,google, it is found that the javascript1.8.5 version of the original implementation of this method, currently ie9+,ff4+,chrome7+ support this method, opera and Safari do not support (MDN instructions).
The role of BIND is similar to that of Apply,call, which is to change the execute context of a function, which is the point of the This keyword when runtime. But the use method is slightly different. A function can be executed later after bind.
Examples are as follows:
Copy Code code as follows:

var person = {
Name: ' Andrew ',
Job: ' Web Front end Developer ',
Gender: ' Male ',
Sayhello:function () {
Return ' Hi, I am ' + this.name + ', a ' + this.job;
}
}
Console.log (Person.sayhello ()); Hi, I am Andrew, a web front end developer
var Anotherguysayhello = Person.sayHello.bind ({
Name: ' Alex ',
Job: ' Back end C # Developer '
});
Console.log (Anotherguysayhello ()); Hi, I am Alex, a back end C # developer

Another example with a parameter:
Copy Code code as follows:

function Add (arg1, arg2, Arg3, Arg4) {
return arg1 + ' + arg2 + ' + arg3 + ' + arg4;
}
var Addmore = Add.bind ({}, ' A ', ' B ');
Console.log (Addmore (' C ', ' d ')); A B c D

If your browser temporarily does not support this method, but you think this is cool, want to use, MDN also give a reference implementation, this implementation is very interesting, the code is as follows:
Copy Code code as follows:

if (! Function.prototype.bind) {
Function.prototype.bind = function (othis) {
if (typeof this!== ' function ') {
throw new TypeError (' Function.prototype.bind-what is trying to being bound ' is not callable ');
}
var fslice = Array.prototype.slice,
Aargs = Fslice.call (arguments, 1),
Ftobind = this,
Fnop = function () {},
Fbound = function () {
Return ftobind.apply (this instanceof fnop? this:othis | | window, AARGS.CONCAT (fslice.call (arguments)));
};
Fnop.prototype = This.prototype;
Fbound.prototype = new Fnop ();
return fbound;
};
}

The last few lines of code, by prototype chain, where Fbound is the subclass of the bind function, and why so, you can look carefully fbound = function () {return ...} This part, where this is the run-time decision, takes into account the case of invoking a function (constructor method) in new ways.
The following example is a good illustration of this, for the convenience of illustration, this example comes directly from MDN:
Copy Code code as follows:

function Point (x,y) {
this.x = x;
This.y = y;
}
Point.prototype.toString = function () {
return this.x + ', ' + this.y;
};
var p = new Point (1, 2);
P.tostring (); 1,2
var emptyobj = {};
var yaxispoint = point.bind (emptyobj, 0);
var axispoint = new Yaxispoint (5);
Axispoint.tostring (); 0, 5
Axispoint instanceof Point; True
Axispoint instanceof Yaxispoint; True

Finally, the article links to facilitate you to further understand
Mdn:https://developer.mozilla.org/en/javascript/reference/global_objects/function/bind

msdn:http://msdn.microsoft.com/en-us/library/ff841995%28v=vs.94%29.aspx
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.