Comparison of several different ways of writing JS self-executing function

Source: Internet
Author: User

Often requires a function to self-execute, but this one is wrong:

function () {alert (1);} ();

The reason is the first half of the "function () {alert (1);}" is treated as a function declaration, not as a function expression, so that the following "();" Become isolated and produce grammatical errors.

According to the above analysis, this piece of code, although there is no grammatical error, but also does not conform to our expectations, because this function is not self-executing.

function () {alert (1);} (1);


The crux of the problem, however, is how to define the code that describes a function expression rather than a function declaration statement.
The correct wording is varied and has pros and cons:

Method 1: The first and last parentheses

(function () {alert (1);} ());


This is the JSLint recommendation, the advantage is, can remind the person reading the code, this code is a whole.
For example, in an editor with syntax highlighting matching, when the cursor is behind the first opening parenthesis, the last closing parenthesis is highlighted, and the person looking at the code can see the whole at a glance.
However, some of the students who write code do not like to add a semicolon after the line will also form some pits, for example, the following code will be reported to run the wrong:

var a=1
(function () {alert (1);} ());



Method 2:function outside parentheses

(function () {alert (1);}) ();


This approach is less of a code integrity benefit than Method 1.

The 3:function method is preceded by the operator, which is common with void.

!function () {alert (1);} ();
void function () {alert (2);} ();



Obviously, add "!" or "+" operators, it is easiest to write.
Plus "void" to knock five keyboard, but heard that there is a benefit is, than add "!" One less logical operation. ----I just heard that, unknown, so.

Finally, on behalf of my personal, strongly support Method 1, that is, the recommended wording of JSLint:

(function () {alert (1);} ());

Reprinted from: http://www.jb51.net/article/31078.htm

Comparison of several different ways of writing JS self-executing 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.