The difference between JavaScript function calls with and without parentheses

Related Tags:1.JavaScript example
2. Prototypes and Inheritance in JavaScript

Introduction: function fun() { return 1; } var a = fun; var b = fun(); In JavaScript, we call the following code a function: function fun() { return 1; } A function is a reference type called function , so the function is an object. The object is stored in memory, and the function name is a pointer to this object. var
function fun ( ) {
return 1 ;
}
var a = fun;
var b = fun( );
In JavaScript we call the following code a function:
function fun ( ) {
return 1 ;
}

【JavaScript function calls with and without parentheses】


A function is an instance of a reference type called function, so a function is an object. The object is stored in memory, and the function name is a pointer to this object.
var a = fun means to copy the pointer of the function name fun to the variable a, but this does not mean that the function itself is copied.
That is: a is the fun function, b is 1
If the function name is followed by parentheses, it means that the code in the function (the code in the curly brackets) is called (executed) immediately.

【JavaScript function calls with and without parentheses】
Without parentheses, the function name is used as the pointer of the function. The name of a function is the pointer of the function. At this time, the result of the function is not obtained, because the function body code will not be run. It just passes the address where the function body is located, so that it can find the function body to execute when needed.
E.g:
window.onload = init ;
init function will not be executed when this line of code is executed. When the browser loads the document, this sentence will be loaded, and it will be told which function to execute after the document is loaded, but it is not executed at that time, until the entire document is loaded. The init () will be executed through the init pointer .
Note: Part of the content of this article is referenced:
https://www.zhihu.com/question/31044040
http://blog.csdn.net/yyx19941129/article/details/49642515
Author: jiankunking Source: http://blog.csdn.net/jiankunking

Copyright statement: The content of this article is contributed by Alibaba Cloud's real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us