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?

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
  • 1
    It's a code block but as you can see it doesn't do much and I've not seen anything like that before - got an example of use? – annakata Oct 12 '10 at 07:42
  • 1
    @annakata, this answer: http://stackoverflow.com/questions/3896577/how-do-i-return-evalcode-and-get-an-object-back-with-javascript/3897576#3897576 – Delan Azabani Oct 12 '10 at 07:43
  • Interestingly, his comment in response to my comment to his answer shows that he holds the misconception that doing `{something}` converts it to an object wrapper of that type, which it doesn't. To do that, we must do `Object(something)`. – Delan Azabani Oct 12 '10 at 07:50
  • Yes, that's correct - it's just a code block (which doesn't even have the same properties as it does in other languages i.e. scoping, so be wary) – annakata Oct 12 '10 at 07:52
  • Tricky, isn't it? Unlike C, in which a block is a single statement and a new scope, JavaScript 'blocks' are only the former. – Delan Azabani Oct 12 '10 at 07:54

3 Answers3

1

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
  • 140,337
  • 36
  • 221
  • 257
1

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
  • 74,572
  • 17
  • 113
  • 180
  • 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 Oct 12 '10 at 07:56
  • I didn't get that from it, but this just speaks to the fact he should have commented what he meant :) – annakata Oct 12 '10 at 08:23
1

This question is super old, but popped up for me in some search results. This sort of syntax represents shorthand property names in ES2015. For example, if you have some local variables defined, you can quickly populate a new object:

let a = 'hello';
let b = 'world';
let c = { a, b };  // equivalent to { a: 'hello', b: 'world' }

So the original example of {window} would return a new object that looked like { window: Window } with a reference to the global window object.

endemic
  • 1,369
  • 1
  • 10
  • 20