Why not write parentheses when JS plus event

Source: Internet
Author: User

Function Al () {
Alert ("I Rub")
}
At this point, Al represents the object of the function, and Al () is directly running.
var Nfun=al; Nfun also points to Al, when Nfun () is the equivalent of Al ().
So let's just say obtn.onclick.
Obtn.onclick is also equivalent to a nfun, except that it is attached to the OBTN tag.
An equal sign is an assignment that, if it is an object, points the object to the variable. Al is the object point, Al () is the function, it is finally something, just look at the function body return what type, AL in an alert, no return anything, then it is equivalent to undefined. So
obtn.onclick=undeined, you do not respond.
But if the inside of Al returns a function such as:
Function Al () {
Alert ("I do it once")
return function () {alert ("I'm Two")}
}
Obtn.onclick=al () is equivalent to
Obtn.onclick=function () {alert ("I'm Two")}

To put it bluntly, parentheses represent the execution of this method, and no parentheses represent assigning the function to a variable.

Why not write parentheses when JS plus event

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.