tags:

views:

39

answers:

2

If the syntax for JavaScript object literals is

{ label: value, label: value, ... }

then why is it that I've seen some people use this in their code?

{window}

What is its purpose? I've tried that and it evaluates to window as it would without the braces. It doesn't even fit in with the object literal notation. Is it a code block?

+1  A: 

That's not the object literal notation. Those are braces which are coincidentally also used to represent a block.

Think,

for(var i = 0; i < 10; i++) 
{

}

See section 12.1 of the ECMAScript spec that explains the grammar and semantics of how a block works.

Anurag
+1  A: 

In the answer you referenced the poster is using a block purely as a form of commenting - he's using the structure it provides to make it clear that it's a separate block of work, but it has no intrinsic value to the code.

Clearly it's use as pseudo-comment is debatable if it confused you. I would tend to avoid it in favour of actual comments.

annakata
Actually, according to the re. poster's answer and comments, he was trying to cast the function primitive into a Function object to make sure that eval() would return it. Of course, this isn't necessary and eval() always returns the result of the last statement in the string passed.
Delan Azabani
I didn't get that from it, but this just speaks to the fact he should have commented what he meant :)
annakata