The description of call () in Mozilla's official website is:
Copy Code code as follows:
The call () method invokes a function or method under the premise of using a specified this value and several specified parameter values.
Call () syntax
Copy Code code as follows:
Fun.call (thisarg[, arg1[, arg2[, ...)]]
Call () parameter
Thisarg
Copy Code code as follows:
The This value specified when the fun function is run. Note that the specified this value is not necessarily the true this value when the function is executed, and if the function is in a non strict mode, the this value specified as null and undefined automatically points to the global object (the browser is the Window object), and the value is the original value (the number , String, Boolean), this will point to the automatic wrapper object for the original value.
Arg1, Arg2, ...
Copy Code code as follows:
The specified argument list.
The call () method in JavaScript
Start the process step-by-step without paying attention to the complex explanations above.
instance of the call () method
So he wrote another hello,world:
Copy Code code as follows:
function Print (p1, p2) {
Console.log (p1 + ' + p2 ');
}
Print ("Hello", "World");
Print.call (Undefined, "Hello", "World");
Both methods have the same output, however, in contrast to the call method, a undefined is also passed.
Next, we'll look at another example.
Copy Code code as follows:
var obj=function () {};
function Print (p1, p2) {
Console.log (p1 + ' + p2 ');
}
Print.call (obj, "Hello", "World");
Just here, we're passing in a undefined, because the undefined in the previous example is due to the need to pass in a parameter. This does not really reflect the use of call, and look at a better example.
Copy Code code as follows:
function print (name) {
Console.log (this.p1 + ' + this.p2 ');
}
var h={p1: "Hello", P2: "World", print:print};
H.print ("FD");
var h2={p1: "Hello", P2: "World"};
Print.call (H2, "nothing");
Call is called by borrowing other people's methods and objects, just like calling your own. In H.print, when you call a function as a method, this points to the related object. Just in this example we did not see whether the H2 was tuned to print, or print called H2. So we cite the example of Mozilla.
Copy Code code as follows:
function Product (name, price) {
THIS.name = name;
This.price = Price;
if (Price < 0)
Throw Rangeerror (' cannot create product ' + name + ' "with a negative price ');
return this;
}
function Food (name, price) {
Product.call (this, name, price);
this.category = ' food ';
}
Food.prototype = new Product ();
var cheese = new Food (' feta ', 5);
Console.log (cheese);
Here we can really see what the object is calling which method. Example, an object instance created with the food constructor will have the name and price attributes added in the product constructor, but the category property is defined in its own constructor.
Copy Code code as follows:
function print (name) {
Console.log (this.p1 + ' + this.p2 ');
}
var h2= function (No) {
THIS.P1 = "Hello";
THIS.P2 = "World";
Print.call (This, "nothing");
};
H2 ();
The H2 here is called the function print as a receiver. As in the food example, in a child constructor, you can implement inheritance by invoking the call method of the parent constructor.
The advantages of the call method are described in "effective JavaScript."
1. Use the call method to customize the receiver to invoke the function.
2. Use the call method to invoke a method that does not exist in the given object.
3. Use the call method to define higher-order functions that allow the user to specify the receiver to the callback function.