OP: At least you recommended that book at the end. That is the sole most useful piece of information in that entire article, and the best resource for anyone learning Javascript. I require any new Javascript programmer on my team read it before starting JS development.
The second most useful thing you could have done is explained to new programmers the use of 'use strict'. It's not always the solution, but it damned well prevents a lot of what you're describing.
Your discussion of variables is flawed, and lacking in detail. The most important thing you could have discussed about variables is that if they are defined anywhere in a scope, they are accessible anywhere in that scope. For example:
function () {
var x = 10;
x += y;
var y = 5;
}
At the end of execution, x will be NaN and y will be 5. Variable declarations get hoisted, but instantiation does not, so the interpreter sees this (effectively):
function () {
var x, y;
x = 10;
x += y; // y is undefined at this point
y = 5;
}
This code is completely valid in JS, because "y" is accessible to the entire scope of its function. Because of this, any sane or informed discussion of Javascript variables must include an explanation of this, and a strong recommendation to define all variables at the top of a function (since they work as if they were all defined there anyways). Failing to explain this is a major failing.
The reason that a variable defined without a var becomes global scoped is because variables default to being defined in the widest scope possible. Thus, defining with a var is necessary because it limits the scope of a variable.
Your point about function declarations is what I generally advise people, but misses out on the most important part of the whole thing: all functions are declared as variables, and can be set or accessed as such. The best reason, IMO, for declaring them as "var foo = function () {};" is to make explicit the nature of the function to the programmer -- programmers coming over from other languages will see this an immediately understand the things you can do to it, whereas seeing the "function foo () {}" declaration style could lead new JS coders to believe (incorrectly) that the function is immutable.
There are some extremely good reasons to have "this" refer to the calling context. It can lead to massive confusion in new coders, and so, yes, I would agree that perhaps new programmers should get more comfortable with closures before doing heavy work with "this". However, avoiding the use of 'this' most likely means that you're overusing closure scoping to crib in a 'that' variable, or else you're not going very OO heavy in your code. While both are entirely valid styles of coding JS, they also drift further away from more standard practices, and can make your code base even harder for new programmers to understand.
I'm a little disappointed that you mentioned OOP, constructor, and prototype, then skipped past providing any examples at all. Your comments about 'this' seem to imply that you have a strong opinion about how these should work, but without actually showing people how you think it should be done, you completely miss a great opportunity to demonstrate proper usage.
Javascript does not treat newlines as a text separator. JS parsers use Automatic Semicolon Insertion to handle cases where there is no semicolon at the end of a line, but, as per the creator of Javascript:
$('button').click // ... ( function () { window.alert(clickmessage); } ); ... Rather than calling the "click" function with that function as an argument, the parser could see you creating a new scope (by encapsulating the function in '()'), and then creating an anonymous function.
I really like to know in which circumstances what you wrote is going to happen. Can you please provide me with a reference or with a working example that demonstrate it?
Ah, I see what you mean. You are correct -- his code as written would not exhibit the problem I described. I'll update the comment with a link to the examples from the google webpage. My intent was to discuss the problem with the syntax, not to demonstrate an actual example of the result. Why? Because it's supposed to be a topic for beginners, and the explanations for why some of these occur is not.
8
u/mirion Feb 23 '13 edited Feb 24 '13
OP: At least you recommended that book at the end. That is the sole most useful piece of information in that entire article, and the best resource for anyone learning Javascript. I require any new Javascript programmer on my team read it before starting JS development.
The second most useful thing you could have done is explained to new programmers the use of 'use strict'. It's not always the solution, but it damned well prevents a lot of what you're describing.
edit -- Several sections below have been edited thanks to feedback from /u/brianloveswords and /u/mikrosystheme
Your discussion of variables is flawed, and lacking in detail. The most important thing you could have discussed about variables is that if they are defined anywhere in a scope, they are accessible anywhere in that scope. For example:
At the end of execution, x will be NaN and y will be 5. Variable declarations get hoisted, but instantiation does not, so the interpreter sees this (effectively):
This code is completely valid in JS, because "y" is accessible to the entire scope of its function. Because of this, any sane or informed discussion of Javascript variables must include an explanation of this, and a strong recommendation to define all variables at the top of a function (since they work as if they were all defined there anyways). Failing to explain this is a major failing.
The reason that a variable defined without a var becomes global scoped is because variables default to being defined in the widest scope possible. Thus, defining with a var is necessary because it limits the scope of a variable.
Your point about function declarations is what I generally advise people, but misses out on the most important part of the whole thing: all functions are declared as variables, and can be set or accessed as such. The best reason, IMO, for declaring them as "var foo = function () {};" is to make explicit the nature of the function to the programmer -- programmers coming over from other languages will see this an immediately understand the things you can do to it, whereas seeing the "function foo () {}" declaration style could lead new JS coders to believe (incorrectly) that the function is immutable.
There are some extremely good reasons to have "this" refer to the calling context. It can lead to massive confusion in new coders, and so, yes, I would agree that perhaps new programmers should get more comfortable with closures before doing heavy work with "this". However, avoiding the use of 'this' most likely means that you're overusing closure scoping to crib in a 'that' variable, or else you're not going very OO heavy in your code. While both are entirely valid styles of coding JS, they also drift further away from more standard practices, and can make your code base even harder for new programmers to understand.
I'm a little disappointed that you mentioned OOP, constructor, and prototype, then skipped past providing any examples at all. Your comments about 'this' seem to imply that you have a strong opinion about how these should work, but without actually showing people how you think it should be done, you completely miss a great opportunity to demonstrate proper usage.
Javascript does not treat newlines as a text separator. JS parsers use Automatic Semicolon Insertion to handle cases where there is no semicolon at the end of a line, but, as per the creator of Javascript:
ASI is (formally speaking) a syntactic error correction procedure. If you start to code as if it were a universal significant-newline rule, you will get into trouble.
A classic example of where this is a problem:
Similar hazards arise with [, /, and unary + and -. Remember, if there wasn’t an error, ASI does not apply.
For more concrete examples, see the Google Javascript style guide.