I'm sorry I did not comment on your post due to lack of reputation.
can you tell me more about "Why can I print foo: 1 in JavaScript"?
If I run this code
var t = {foo: 1};
It will become a property for the t object. The same behavior will be implemented if you use this code
var t = {foo: 1, bar: 2};
You can access it using "t.foo" and "t.bar", and it will return the value "1" or "2".
Here you can read the explanation of the "object" JavaScript objects
source share