While debugging a web SPA issue, I stumbled on something to which I could not find concrete references online: missing comma separator between function expressions in javascript. Here's the details:
This works - explicit comma as a separator is there (note - intentionally on one line):
var f1 = function() { console.log(1); }, f2 = function() { console.log(2);}
This however doesn't seem to work, trying it in the Chrome console (again - one-liner on purpose):
var f5 = function() { console.log(5); } f6 = function() { console.log(6);}
VM37860:2 Uncaught SyntaxError: Unexpected identifier
at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
at Object.InjectedScript.evaluate (<anonymous>:694:21)
Then this one seems to work - notice missing comma:
> var f3 = function() {
console.log(3);
}
f4 = function() {
console.log(4);
}
< function f4()
> f4()
4
< undefined
> f3()
3
< undefined
I am looking for explanation or a reference to one as to why multi-line but missing comma seems to work.
The ramification was that a missing comma in our source that slipped into the build caused unexpected behavior in otherwise syntax-correct script bundle (the script snippet containing the missing comma piece is bundled together with many other components on the server side and emitted as a single script tag to the browser). I.e. Chrome and FF were not reporting syntax errors, yet script behavior was incorrect (it's a complex knockout.js-based SPA but it seemed as if the wrong function out of many functions with same name, but different scope, was being called; version of knockout used is 2.x).
Regardless - I am interested if someone can explain the console behavior from pure Javascript / Chrome console point of view, outside of the scope of knockout-based SPA.
Javascript will assume semicolons at some line breaks. Ugh. So your second variable was a global, not part of the
var
. http://inimino.org/~inimino/blog/javascript_semicolons