-
Notifications
You must be signed in to change notification settings - Fork 26.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unclear code example in variables section #136
Comments
I'm not sure about unclear, but what I saw hear was a counterexample of "Assign variables at the top of their scope." Here, if the test is false, we'd have at least one useless declaration, function call and assignment. |
Variable will be hoisted anyway. But getName will not be executed. I would go with an alternative example: https://gist.github.com/yaroslavya/8957634 |
+1 |
This does seem like a counterexample. E.g., whereas in the former example,
The variable In the "counterexample", whereas here the if block uses |
All variables was declared at the start function, because for this way, we avoid the hoisting problem :). |
The reason why you place
before the veritable declaration
is that the function is expecting arguments that are probably required by the I think the confusion here comes from the over simplified example. You always want to declare your variables on top of the function block, but if your function depends on an argument you want to skip the execution of any code if that argument requirement is not met. |
You can always do var name;
if (!arguments.length) {
return false;
}
name = getName(); |
... I believe we are all thinking and saying the same things now in different words. Can someone submit a pull request? (too lazy/busy here to be the one :) ). |
Unclear code example in variables section. Variables should be declared at the top of their scope to avoid declaration hoisting.
Closing in favor of #265 |
At the bottom of the variables section, this code example is given:
At the top of that code block it says:
It's not clear what point the code example is trying to make, as it doesn't reflect the statement made in that comment. Plus, the
name
variable is defined but never used.The text was updated successfully, but these errors were encountered: