in learning JavaScript, the use of call () and apply () is always difficult to understand, I believe many small partners and I have the same idea. Recently in the Rhino book, the two functions have a hard understanding, finally some clues, understand the basic usage of two functions, and now share with the small partners, said the wrong still hope to correct.
First, call
Syntax: Call ([thisobj[,arg1[, arg2[, [,. ArgN] ]])
Definition: Invokes one method of an object, replacing the current object with another object.
After reading the syntax and definition is still difficult to understand, the call function can accept a number of parameters, where the first parameter is the object of the current operation, the following parameters are the parameters of the method called, so it is difficult to understand, directly on the code:
1 functionTeacher (name, age) {//define a constructor teacher2 This. Name =name;3 This. Age =Age ;4 This. Intro =function() {5Alert ("My name is" + This. Name + "," + "I ' m" + This. Age)6 }7 }8 9 functionStudent (name, age) {//define a constructor studentTenTeacher.call ( This, name, age);//student called the teacher method . One } A varStudenta =NewStudent ("Lily", 19); Instantiate a Studenta, passing in Parameters -Studenta.intro (); Call Studenta's Intro method output: My name is Lily,i ' m 19
In the example above, Studenta is instantiated through the constructor student, which should inherit all the inheritable methods of the student constructor, but we see that the student function does not have any method, but the student call, can be said to inherit the teacher method, Therefore Studenta also has the intro () method. Where line tenth of the code plays a key role Teacher.call (This, name, age), the purpose of this sentence is to make the current object call another object method, The This keyword here refers to the student () object, which is instantiated after this refers to the Studenta object, and the subsequent arguments are teacher parameters that are passed in. Perhaps the above example is not very common, but we often see this code:
1 var str= "Hello beauty"; 2 Array.prototype.join.call (str, "!"); // you, good, beautiful, girl .
The Join method is an array-specific method that is defined in the prototype of the array () constructor, so the array we instantiate inherits this method, but the string Str does not have a join method, but here we borrow the array's join method to process the string. The expression on the left side of the call is the method of the borrowed object, and the first parameter object in call () borrows the method of the expression object on the left side (I don't know if I can understand it or not).
Second, apply
The Apply () method works the same as the call method, except that the parameters passed in are different.
Syntax: Apply ([Thisobj[,argarray]])
Definition: A method of applying an object that replaces the current object with another object.
The first parameter of apply is the same as call, and the second argument is an array object (which can also be an array of classes), and instead of passing in a parameter as a single argument list, all parameters are placed in an array and passed in together. Here we have to talk about the arguments object, which exists in each function and is a class array object that consists of all the arguments of the function. So the second parameter of apply is passed into the arguments object. Look at the following example:
1 var arr=[2,3,12,5,234,199,21,1000,2432]; 2 var max=Math.max.apply (Math,arr); 3 // 2432
There is no way to find the maximum value in the array, but Max can be found in the Math object, so we use the Apply method to let the array inherit the Max () method of math, thus finding the maximum value in the group, and the same method can be replaced with the call () method, but the writing is more troublesome, Not easy to read, the code is:
1 var max=math.max.call (math,2,3,12,5,234,199,21,1000,2432); 2 // 2432
The basic use of call and apply is these, of course, the beauty of these two functions far more than these, welcome the great God to add.
Understanding of call () and apply () in JavaScript