When I run this code, it works, as you would expect:
var b = {}
b['foo']=[]
b['foo'].push(1)
But when I try doing this as a one-liner, like this:
var b = {}
(b['foo']=[]).push(1)
It throws: Cannot set properties of undefined (setting 'foo')
, even though b
is very clearly defined.
Could someone explain this behavior? Thanks in advance.
Edit: For some reason, adding the semicolon fixes the issue, which confirms that the compiler was in fact trying to run it as a function. But now I have another question: Why did it throw an error saying b
is not defined, instead of b
is not a function?