I also doubt its enforcing of good semicolon habits, considering that semicolons are not needed after many a statement in C-like languages, including Javascript.
This seems more of enforcing one's personal opinion than anything else, to me.
This seems more of enforcing one's personal opinion than anything else, to me.
(function(){
var foo = 1;
console.log(foo);
})() // Semicolon missing!
(function(){
var bar = 1;
console.log(bar);
})()
It's ambigous for the parser so this will throw a TypeError: "undefined is not a function". So overall I don't think is a stylistic choose but a safety one.What I'm still arguing for is that "enforcing semicolon habits" is not a compelling argument for using function expressions over function declarations. (Perhaps you're talking about semicolons in general, which is something I would rather not get into :)
And function declarations can create ambiguous code, because functions are first class citizens on JavaScript it means code like this is valid:
var foo = function (bar){
console.log(bar);
}
but for the lacking semicolon it throws an error if it is followed by: (function(){
var foo = 1;
console.log(foo);
})() ;(function(){
var foo = 1;
console.log(foo);
})()
In Lua, whitespace is very nearly all the same. For example: a = 1 b = 2
c
=
3
is valid Lua code. However, lines that start with parens have the same ambiguity: a = something
(expression)(arguments)
--Could be:
a = something(expression)(arguments);
--Also could be:
a = something;
(expression)(arguments);
The parser will complain about the ambiguity. If the intention is to have 1 statement, they should probably be on the same line, otherwise the convention is to put the semi-colon at the beginning of the second line: a = something
;(expression)(arguments) The quick brown fox jumps over the lazy dog
. The fox didn't say anything that day
, because in all honesty what does the fox say
? function foo() {
console.log('bar');
}
And when referring to a function expression I'm talking about this: var foo = function () {
console.log('bar');
};
The latter needs to be terminated with a semicolon to prevent ambiguity (in some situations), while the former does not. This for example is valid JavaScript: function foo() {
console.log('bar');
}foo=5