Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    var a = x:1
This is invalid. Stop guessing. Read the language spec. Best by far is "Definitive Javascript O'Reilly"

{} is a block of statements. For example,

  {doSomething();doAnotherThing()}
{} when used as an expression, is object literal notation. For example,

  var a = {foo:23,bar:99};
If you want to be explicit that something is an expression, enclose it in parenthesis...

  Console>{foo:1}
  1                // "{}" = code block, "foo:" = label.

  Console>({foo:1})
  {foo:1}
Return an object:

  function foo() {
    return {bar:23, baz: function() {return 89}};
  }


Returning an object from a function was not the problem - it was "returning" from a Script (as executed by the NodeJS Script class). So essentially the last value in the script, I suppose.

I like the ({x:1}) shortcut, thanks!


I don't quite understand what you mean by 'returning from a script'. That doesn't make too much sense within Javascript itself, if it's a case of the last expression being considered the return value (I haven't used NodeJS), then you would want:

  doSomething();
  ({x:1});


    eval("{x:1}") // returns 1
    eval("({x:1})") // returns {x:1}




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: