Bind usage in javascript

Source: Internet
Author: User

I saw an interview question a few days ago. The question is as follows:
What do you know about apply, call, and bind in javascript?
First, apply and call are common things, but for bind, I am stunned, because this word is a frequently used method in jquery, used to bind events to DOM elements.
To find out this strange and familiar bind, google found that the original method was implemented in javascript1.8.5. Currently, IE9 +, ff4 +, and chrome7 + support this method, opera and safari are not supported (the description on MDN ).
The role of bind is similar to that of apply. call is to change the execute context of the function, that is, the point of this keyword in runtime. However, the usage is slightly different. After a function is bind, it can be executed later.
Example: Copy codeCode: 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

Example with parameters:Copy codeThe Code is 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 does not support this method for the time being, but you think it is cool and you want to use it, the MDN also provides reference implementation. This implementation is very interesting. The Code is as follows:Copy codeThe Code is 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 be bound is not callable ');
}
Var fSlice = Array. prototype. slice,
Using GS = fSlice. call (arguments, 1 ),
FToBind = this,
FNOP = function (){},
FBound = function (){
Return fToBind. apply (this instanceof fNOP? This: oThis | window, batch Gs. concat (fSlice. call (arguments )));
};
FNOP. prototype = this. prototype;
FBound. prototype = new fNOP ();
Return fBound;
};
}

The last few lines of code are in prototype chain mode. fBound is a subclass that calls the bind function. For more information about this implementation, see fBound = function () {return ...} in this part, this is determined by the runtime. Here, we mainly consider the situation where the new method is used to call a function (the constructor method.
The following example shows this point. For convenience, this example is directly from MDN:Copy codeThe Code is 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 link is provided for you to learn more
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

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.