Function expression ending with ; vs. not
When reading others code I have seen functions written both ending with a semi-colon and not ending with a semi-colon.
with:
var testFunction = function() {
// magic happens here
};
without:
var testFunction = function() {
// magic happens here
}
- ?: Is one more "technically" correct than the other?
- ?: Is there a speed advantage to one?
- ?: Do browsers not care and so it's just a style bleeding over from another language or an old format that used to be required for JavaScript functions?
- ?: #{question I hadn't considered开发者_运维百科 to ask here but you think I should have}
Update: I also found this very useful => https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope
At it's root, what you have there is an assignment statement, and according to the Google Javascript Style Guide, all statements should end in a ;
. Especially assignment statements. JSLint requires ;
or else it won't pass.
There are two ways to define functions:
var testFunction = function() {
// magic happens here
};
and
function testFunction(){
// magic happens here
}
I believe it's better to end with a ;
when you define it using a statement (first option), but not as a function definition (second option).
JS Parsers are pretty flexible, so YMMV.
I would consider it technically correct to have the ;
semicolon terminate the function because you are executing a declarative statement. Though not required by the grammar, it is generally accepted that variable declarations be terminated with a ;
semicolon. In this case, the "variable" just happens to be a function.
// As you do this
var i = 0;
// Also do this
var obj = function() {
// aforementioned magic
};
It may also be worthy to note that, when declaring objects, each field is separated with a comma ,
.
var obj = function() {
magic1 : function() {},
magic2 : function() {}, // <-- Similarly, comma is optional here...sorta
// See Marcel's comment below.
};
Of course, for simple functions declarations, it could be considered "more technically correct" to not have it since it is not a linearly executed statement. (See excellent counterpoint in Google style guide, etc) In this case, the semicolon ;
gets interpreted as an "empty" line of code, not an expression termination token.
With simple function declarations, leave it off:
function doMagic() {
// aforementioned magic
}
Is one more "technically" correct than the other?
No. Technically you're assigning a value to a variable that happens to be a function, just the same as var x = 10
. You can declare this with or without the ;
and it's equally correct either way.
Is there a speed advantage to one?
I would be immensely surprised if there was a noticeable difference.
Do browsers not care and so it's just a style bleeding over from another language or an old format that used to be required for JavaScript functions?
Browsers don't care, it's just a quirk of the language.
question I hadn't considered to ask here but you think I should have
Which one is more readable, you say? I personally prefer to end statements with a ;
because it feels right and is more clear about what you're doing. I come from Java-land, where this is required, but even when it isn't I think it's clearer to explicitly end the statement with a ;
, so I include it.
But, as with any code style argument, it quickly becomes a matter of personal choice, and there be flamewars. Proceed with caution.
精彩评论