The best JavaScript guide ever
developer.mozilla.org
developer.mozilla.org
And with that, I close my thick O'Reilly book for the rest of the day.
http://oreilly.com/catalog/9780596517748 http://www.youtube.com/watch?v=hQVTIJBZook
I also strongly recommend his work for a better understanding of Javascript.
[edit] title updated
Noting in the title that this is Mozilla's JS reference is enough to give it credence and is still objective.
Or apparently, on the Processing XML with E4X page, there are errors in the page: /content/body/div[2]/pre[1]/@function, reference to undefined name 'syntax': line 1, column 1 where every code sample should be.
It's under a CC license, so I am hoping that someone will make a better interface for the content some day.
My goal was to return something like {x:1} (an object) from a script. It never worked. If I type that into a console, it returns "1". If I type x:1 into the console, it also returns 1. So I guess the "{}" is just interpreted as a block, returning the value of the x:1 expression. But what does x:1 mean? I am guessing it might just be a bug in the interpreter? Or when is something an object, and when just a block? var a = x:1 doesn't work, btw. this.x is also still undefined after this.
The only way I could return my wanted object was by doing var y = {x:1};y;
(This was supposed to be the return value of a Script in NodeJS, using these silly values because it was a unit test).
function asdf(){
return {x:1};
}
It's a less verbose way of doing this: function asdf2(){
var myObj = new Object();
myObj.x = 1;
return myObj;
}
Outside of a object written in shorthand notation, x:1 is treated as a label (x:) followed by a value (1). In general labels are not used in JS, and it's probably a good idea to avoid them.Relatedly, to "fake" a block in JS, wrap it in a function:
(function(){
var a = 1;
// do something with a
}());
// out side of the "block" a is undefined (or whatever value it was before the "block")
[Edited for accuracy and clarity]I have actually used labels in JS before, which resulted in a lot of questions from my colleagues.
I think the loop: for(...){break loop;} thing is acceptable (if for can not be avoided anyway).
I think what you mean to say is that Javascript doesn't have block scope for variables. It has blocks.
if (...) {
// block
}
It turns out that blocks don't need to follow a control flow construct like "if", "while", etc, they can stand on their own anywhere a statement can.What's happening in the above case is the syntax for an object literal and a block is ambiguous. It's normally not a problem since you don't have object literals standing on their own as statements, you do something with them as an expression. On a console (or any time you eval an expression) you should wrap it in a parenthesis to disambiguate it:
({x:1}) // expression (object with "x" property equal to "1")
{x:1} // statement (label "x" followed by statement "1")You can toss extra curly braces in the code and it is a "block" but unless they are preceded by an "if", "for", or something similar, they don't do much. They do not return values and they do not affect variable scope. Functions do both, hence my last example.
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}};
}I like the ({x:1}) shortcut, thanks!
doSomething();
({x:1}); eval("{x:1}") // returns 1
eval("({x:1})") // returns {x:1}==== if (true) { function foo(){ return 1; } } else { function foo(){ return 2; } } foo();
firefox return 1 webkit return 2 Other engines return an error (that is what is suppose to happen according to the specification)