{}+{} incosistency

Boris Zbarsky bzbarsky at mit.edu
Wed Apr 13 16:16:37 UTC 2016


On 4/13/16 12:08 PM, Boris Zbarsky wrote:
> It sounds like all the actual JS engines involved get all of this right;
> the only difference is whether evaluation in the browser's console
> happens in an expression context or in a statement context.

And in particular, it seems like most browsers evaluate console stuff in 
a statement context, but Chrome is somewhat inconsistent about it.  Some 
experimentation suggests the following is happening, for various input 
strings:

1) "{}": expression context; claims to be an Object.
2) "   {}": expression context.
3) "var x = 1": statement context; defines a property named "x" on
    the global.
4) "let x = 1": statement context; creates a binding for "x".
5) "function f() {}": statement context; defines a property named "f"
    on the global.

I haven't experimented with more interesting whitespace preceding a '{' 
but it looks like the heuristic is to use expression context if the 
first non-whitespace character is '{' and to use statement context 
otherwise or something.

-Boris


More information about the es-discuss mailing list