Introduction to the call () method in Javascript
This article mainly introduces the call () method in Javascript. This article describes the Call () syntax, Call () parameters, the call () method in Javascript, and Call () for more information, see
On Mozilla's official website, the following is an introduction to call:
The Code is as follows:
The call () method calls a function or method when a specified this value and several specified parameter values are used.
Call () syntax
The Code is as follows:
Fun. call (thisArg [, arg1 [, arg2 [,...])
Call () Parameters
ThisArg
The Code is as follows:
This value specified when the fun function is running. It should be noted that the specified this value is not necessarily the true this value during function execution. If this function is in non-strict mode, the value of this specified as null and undefined automatically points to the Global Object (window object in the browser), and the value is the original value (number, String, Boolean value) this will point to the automatic packaging object of the original value.
Arg1, arg2 ,...
The Code is as follows:
The list of specified parameters.
Call () method in Javascript
Start the process step by step without paying attention to the complex explanations above.
Call () Method Instance
So I wrote another Hello, World:
The Code is as follows:
Function print (p1, p2 ){
Console. log (p1 + ''+ p2 );
}
Print ("Hello", "World ");
Print. call (undefined, "Hello", "World ");
The two methods have the same output results. However, the call method also transmits an undefined.
Next, let's look at another example.
The Code is as follows:
Var obj = function (){};
Function print (p1, p2 ){
Console. log (p1 + ''+ p2 );
}
Print. call (obj, "Hello", "World ");
Here, we pass in an undefined, because the undefined in the previous example is required to pass in a parameter. The call usage is not actually reflected here. Let's look at a better example.
The Code is 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 using others' methods and objects, just like calling your own. In h. print, when a function is called as a method, this points to the relevant object. In this example, we did not understand whether h2 has called print or whether print has called h2. The Mozilla example is referenced.
The Code is as follows:
Function Product (name, price ){
This. name = name;
This. price = price;
If (price <0)
Throw RangeError ('could not 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 which object is called and which method. In this example, all object instances created using the Food constructor will have the name and price attributes added to the Product constructor, but the category attributes are defined in their respective constructors.
The Code is 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 ();
Here h2 is used as a receiver to call the function print. As in the Food example, in a sub-constructor, You can implement inheritance by calling the call method of the parent constructor.
The advantages of the Call method are described in objective JavaScript.
1. Use the call method to customize the receiver to call the function.
2. You can use the call method to call a method that does not exist in the specified object.
3. The call method can be used to define a high-order function that allows the user to specify a receiver for the callback function.