The non-normal invocation mode of JS function

Source: Internet
Author: User

As we all know, there are two ways to declare a function

function FnA () {alert (' msg ');} Declarative definition function var FnB = function () {alert (' msg ');} function assignment expression definition function

Usually, the way we call a method is functionname ()

However, if we try to add () to the end of a defined function, the parser is incomprehensible.

function msg () {alert (' message ');} ();//parser is not understandable

The calling method of the defined function should be MSG (); So why wrap the function body part in ()?

It turns out that using parentheses to define the function body, the parser will invoke the definition function in the form of a function expression. In other words, any function that can be turned into a function expression can make the parser call the definition function correctly. and! is one of them, and the ~ +-| |, etc. all have this function.

In addition, use! Probably more of a habit problem (omitting one character than the double parenthesis), different operators, the performance is different.


The non-normal invocation mode of JS 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.