JavaScript in the Call,apply,bind method is not easy to understand, in fact, the idea behind is not very complicated, I hope this article can help you better understand the 3 like, and seemingly very mysterious method.
Instead of using a keyword to point out the essence of the thought behind them, the keyword is: this
Because the programmer is usually familiar with C + +, first borrow C + +, briefly say this.
class, you can use this to access the members of the current class, but the problem is that the parameters of the member function do not have this parameter, such as:
The animal object calls the parameterless and single-parameter member function eat. You can use this to access other members in the Eat method. The problem is that eat is a "no parameter" or "single parameter" method, and does not have a formal parameter called this.
The reason is that the compiler extends the member functions of the class.
A.eat (); The compiler will rewrite: Animal::eat (&a); A.eat ("meat"); The compiler will rewrite it: animal::eat (&a, "meat");
Each member function of the class (except the static member function). Because static is a class rather than an object, static and this are inherently semantically different) and have an additional, implicit formal parameter, this. When the member function is called, the parameter this is initialized to the address of the object that called the function.
Pulled away, pull back the theme ... Let's look at the call method in JavaScript in Function.prototype:
Just like C + +, the receiver of a function or method is bound to this, but sometimes a custom receiver is required to invoke a function or method. The call method is used to change this.
function Class1 () { this.name = "Class1"; This.showname = function () {alert (this.name);}} function Class2 () {
If you call ShowName directly on the Class2 object, you must have an error:
C2.showname (); Error
The compiler will rewrite it: Class2::showname (&C2), obviously there is no ShowName method in Class2 error, it is not surprising that everyone can understand.
But ShowName method is too tempting, anyway want to use it? There are two ways, a natural is to define a ShowName method in Class2, which is not bad, but sometimes it seems troublesome, and repeated code maintenance is also a problem. Another way to solve this problem is to use call:
C1.showName.call (C2);//class2
The compiler will rewrite the following: Class1::showname (&C2), the hidden formal parameter this is the default object that points to Class1, and now the call method changes this to the object that points to Class2.
This applies the ShowName method entity defined in Class1 to the Class2 object, which replaces the C1 context with the C2 context
After understanding call,apply is solved, because the two are essentially the same, the difference lies in the external performance:
Object.call (this, arg1,arg2,arg3) = = Object.apply (this, arguments)
From the second parameter, the call method parameters are passed to the borrowed method as arguments, and apply is passed directly into an array, and the argument list of the last borrowed method is the same.
Because the ShowName method of the above example has no parameters, it is the same with call and apply:
C1.showName.call (C2);//class2c1.showname.apply (C2);//class2
Finally,Bind。 Call and apply are the same no exception to change the This,bind, but the method is returned after changing this, so the semantics of BIND is: To obtain a method with a definite receiver.
It essentially gets a method that can be called directly or passed as a parameter to a higher-order function.
If the above example is called directly, bind can be changed to:
C1.showName.call (C2);//class2c1.showname.apply (C2);//class2c1.showname.bind (C2) ();//class2
Do you see a pair of parentheses behind bind? If there is no that pair of parentheses, c1.showName.bind (C2); Just represent a method, and then there is no more. Plus that pair of parentheses is a direct call to the "method returned after binding the receiver through bind"
If you can only invoke directly, then with call and apply, bind has no value, so another function is to pass as a parameter to the higher order function, a common example is to use the BIND implementation of the function of the curry (the function of a subset of functions and its parameters is called function currying):
function simpleurl (protocol, domain, path) { return protocol + "://" + domain + "/" + Path;}
Above is a very common function for generating URL addresses. But you think in fact the first two parameters are usually fixed, but the third parameter to change, you can:
var urls = paths.map (path) { return Simpleurl ("http", "CSDN", path);//The first two parameters are fixed, only the 3rd parameter will change});
Use bind to simplify the above code:
var urls = paths.map (Simpleurl.bind (NULL, "http", "CSDN"));
The first parameter of BIND is the receiver of the method, since Simpleurl does not have this, so it is set to NULL, which means that the receiver is ignored. This way bind the single parameter path call Simpleurl function to the Simpleurl ("http", "Baidu", path) function.
Introduction to Call,apply,bind methods in JavaScript