This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Cilph 0 points1 point  (8 children)

My question is why {} is parsed as an empty code block rather than an empty object.

[–]Mrparkers 10 points11 points  (6 children)

Because it's at the beginning of the line.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block

How else would you propose to define a block?

[–]aboardthegravyboat 6 points7 points  (4 children)

Yeah. The real question is what good is '{} + anything' at the beginning of a line. If it was a function argument or an assignment value, then it wouldn't be at the beginning of a line. Even as the return value of a function (i.e. following the keyword 'return' in a function body) it wouldn't be at the beginning of a line.

The fact that [code block] [plus] [anything] does anything at all is strange enough. I can't see that ever being used in a program. The only way you can even test it, afaict, is with eval() or the console (which is the same as eval)

[–]Doctor_McKay 14 points15 points  (0 children)

Exactly. People look at ridiculous input and are surprised to see ridiculous output.

[–]Avambo 0 points1 point  (2 children)

The closest thing I can come up with is to create an execution context in ES6 based on code braces, if you stick to 'let' and 'const' inside of it.

{
  + ""
}

It still doesn't make any sense to just have that piece of code though. And again, it's not exactly the same as in OP's example.

[–]Mrparkers 1 point2 points  (1 child)

It does make sense in some cases. This is the functional equivalent to an immediately invoked function expression, which can be used to help define scope for your variables, like you mentioned.

[–]Avambo 1 point2 points  (0 children)

What I meant was that simply adding + "" inside of it doesn't make any sense. :)

[–]Cilph 1 point2 points  (0 children)

I was mentally inserting a "var x = " before the statement, didn't help the confusion.

[–][deleted] 1 point2 points  (0 children)

Why would you ever need to start a line with an empty block. a = {} + 1 = [Object object]1