Call () method in Javascript, javascriptcall

Source: Internet
Author: User

Call () method in Javascript, javascriptcall

On Mozilla's official website, the following is an introduction to call:
Copy codeThe 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
Copy codeThe Code is as follows:
Fun. call (thisArg [, arg1 [, arg2 [,...])

Call () Parameters

ThisArg
Copy codeThe 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 ,...
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe 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.
Copy codeThe 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.
Copy codeThe 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.

Copy codeThe 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.

Related Article

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.