The difference between a JavaScript function and no parentheses

Source: Internet
Author: User

<! DOCTYPE html>

  

E.  AddEventListener("click",demo2)  ,Demo2 after the parentheses into the open page directly pop-up dialog box without having to click the button,  Without parentheses, the event pop-up dialog is triggered by a click of a button , and I feel that the problem is quite common and confusing, as well as a summary.

function Fun () {
Return 5
}
var a = fun;
var B = Fun ();
A simple function is defined first, which is an instance of a function reference type, so the function is an object. Objects are stored in memory, and the function name fun is a pointer to the object.
var a = fun, which is equivalent to copying a function name pointer to a variable a, the function itself is not copied.
However, if you add parentheses to the fun, it's equivalent to executing immediately, which means that B gets the return value of the function directly.
Then the problem with the above code is:
An event trigger is a function that is called by a user to perform an action that is invoked by the event listener. The AddEventListener () method accepts three parameters, where the second parameter is the function that should be executed when the event is triggered, and if no event is triggered, the function will not be called. If you add parentheses, it is equivalent to not having to listen to the previous event to trigger.



The difference between a JavaScript function and no parentheses

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.