Four ways to call a function

Source: Internet
Author: User

In JavaScript, there are 4 modes of invocation:

    • Function call pattern
    • Method invocation Pattern
    • constructor invocation Pattern
    • Indirect invocation mode, via call () and apply ()
1. Function call mode

Normal function call patterns, such as:

function printprops (o) {  ...  } Printprops ({x:1});

In a call,

(1) The result of each parameter expression evaluation is passed as an argument to the formal parameter defined when the function is declared;

(2) This is bound to a global variable

var myObject = {value:1= 2; myobject.printpropsfunction() {  var  function() {        console.log (this.value);     };
  Printvalue ();
  Console.log (this.value);} MyObject. Printprops();

The result of this operation is:

21st

We notice that when the Printvalue () function executes, the This.value value is 2, that is, this is the global object, not the MyObject.

(3) Return value: The return value of the function becomes the value of the invocation expression. I. If the function returns to the end of the interpreter, that is, there is no statement executed to return XXX. The return value is undefined. II. If the function returns because the receiver executes to the return XXX statement, returns the value after the return. III. If there is no value after the return statement, which is return, returns undefined.

2. Method invocation Mode

When a function is saved as a property of an object, it is called a method.

(1) The processing of parameters and return values is consistent with the function invocation;

(2) Call context this is the object

function Printvalue () {  Console.log (this. Value);  } var value=1; var myObject == printvalue; // as a function call Printvalue (); // Call MYOBJECT.M () as a method ;

The result of the operation is:

12

We notice that when Printvalue is called, this is a global object that prints the global variable value 1. However, when MYOBJECT.M () is called, this binds the object that the method m belongs to, so the printed value is Object.value, which is 2.

3. Constructor invocation mode

If a function or method call is preceded by a new keyword, it constitutes a constructor call. Such as:

function F () {...} var New F ();

(1) Parameter processing: general case the constructor parameter processing and the function call pattern are consistent. However, if the constructor does not have formal parameters, the JavaScript constructor call syntax allows you to omit the argument list and parentheses.

For example, the following two lines of code are equivalent.

var New Object (); var New Object;

(2) The calling context of the function is the newly created object.

function Base (value) {  this. Value =value;}  var value =1; var New Base (2); Console.log (value); Console.log (O.value);
Base (3);
Console.log (value);
Console.log (O.value);

Operation Result:

12
3
2

I. The first call to the base () function is called as a constructor, at which point the call context this is bound to the newly created object, that is, O. So the global variable value does not change, and O adds an attribute value of 2;

II. The second call to the base () function is called as a normal function, at which point the call is bound to the global object, and the window is in the browser. So the value of the global object is changed to 3, and the value of the O property is not changed.

(3) The constructor usually does not use the return keyword, and the return value is the new object. The invocation expression value is the object, if the constructor displays the return statement with the returned object. If the constructor uses a return statement but does not specify a return value or returns a raw value, the return value is ignored and the new object is used as the result of the call.

Function call pattern Method invocation Pattern constructor invocation Pattern Indirect invocation pattern
Argument handling Parameter expression run result passed as argument to function parameter Consistent with function invocation pattern

General, consistent with function invocation pattern

However, if the constructor does not have formal parameters, the JavaScript constructor call syntax allows omitting the argument list and parentheses

The call () method uses its own argument list as an argument to the function

The Apply () method requires the parameter to be passed in as an array

Invoke context Point to global variables Point to the object to which the method belongs Point to the newly created object Allows the specified call to be displayed with the this value, which is the first parameter in the calling () or apply ()
return value

I. No execution to the return statement, return undefined

Ii. execute to the return XXX statement, returning the value after return

Iii. Retrun No value, return to undefined

Consistent with function invocation pattern

The constructor usually does not use the return keyword, and the return value is the new object.

If the constructor displays an object using the return statement, the call expression value is the object.

If the constructor uses a return statement but does not specify a return value or returns a raw value, the return value is ignored and the new object is used as the result of the call.

Four ways to call a function

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.