3 ms·
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,
by points 16y ago
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}};
}
- Tichy 16y agoReturning 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!
- points 16y agoI 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});
- tlrobinson 16y agoeval("{x:1}") // returns 1 eval("({x:1})") // returns {x:1}