Why does this work:
a = []
a.push(['test']);
(function() {alert('poop')})()
But this gives the error “number is not a function”:
a = []
a.push(['test'])
(function() {alert('poop')})()
The only difference is the semicolon at the end of line 2. I’ve been writing JavaScript for a long time now. I know about automatic semicolon insertion, but I can’t figure out what would be causing this error.
Take a look at this example of chained function calls.
Look familiar? This is how the compiler/interpreter views your code.
Detail
Here is a portion of the grammar used to describe call expressions.
Essentially each group (…) is considered as Arguments to the original MemberExpression
a.push.Or more formally
CallExpression( CallExpression( CallExpression( MemberExpression( a.push ), Arguments( (['test']) ) ), Arguments( (function() {alert('poop')}) ) ), Arguments( () ) )