Many times I've seen a semicolon used after a function declaration, or after the anonymous "return" function of a Module Pattern script. When is it appropriate to use a semicolon after curly braces?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Semicolons go at the end of lines that do not end in a curly brace or to separate statements on the same line. It does no harm to use them after a closing brace, or to wear suspenders and a belt, but it does look a little nerdy.
You never need to; you always can (except before
else
andwhile
).Explanation:
Unfortunately, Javascript semicolons are optional.
Therefore, you never need to add a semicolon.
It is (very) good practice to terminate every statement with a semicolon.
The only statements that end with a
}
are statements ending with an object literal (e.g. JSON) or function expression.Therefore, best practice is to put semicolons after the following two braces (only):
If we have a self-invoking function, we need to put a semicolon before it, otherwise it becomes part of the previous assignment statement. Consider the following:
This will produce the following output:
...plus a JavaScript error reported by the browser:
testOb.testMethod is not a function
This is certainly not what we intended. Why is
testMethod
running immediately, before we have even instantiated the class? And why does it no longer exist when we want to call it as a member method?What is happening is that
testMethod
is being assigned not our function definition, but the return value of the function definition. And the function definition itself is being run anonymously. This is how:testClass
constructor and the member methodreport
are successfully defined/assigned.testMethod
, the()
surrounding the following self-invoking function becomes an invocation operator, which causes what we think is our definition oftestMethod
to become an anonymous function that is invoked immediately, and the return value of the following anonymous function becomes its parameter list. This explains the order of printed output - our self-invoking function is run first as it is evaluated as a parameter.testMethod
, and not the function definition. This is confirmed by our printing of the type and value oftestMethod
.testClass
is successfully instantiated astestOb
and itsreport
method works as intended, proving that the class definition is otherwise intact.testMethod
, we are told by the interpreter that it is not a function - and rightly so, because it is a number with the value 2.If we put a semicolon after the definition of
testMethod
, it will separate its assignment from the calling of the self-invoking function, and we will have the result we expected:Or we could even put it directly before the anonymous function:
But I suggest that since the problem is due to the lack of a semicolon at the end of an assignment statement, we should perhaps make a habit of always putting a semicolon after defining functions in this way. i.e. all of my functions above should have a semicolon after the closing brace, because they are all assignments of anonymous functions.
I know this thread is old but couldn't resist to share this code:
Will return "Property of object [object Object] is not a function". Because it will actually be executed as:
You use a semi-colon after a statement. This is a statement:
because it is a variable assignment (ie creating and assigning an anonymous function to a variable).
The two things that spring to mind that aren't statements are function declarations:
and blocks:
Note: that same block construct without semi-colon also applies to
for
,do
andwhile
loops.It matters too when you intend to minify your code.
So I personally add one after every
}
where ASI would insert one.I wrote a post about ASI in JavaScript.