-
Notifications
You must be signed in to change notification settings - Fork 4k
corrected translation to english errors in 'var' lesson #456
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ In the very first chapter about [variables](info:variables), we mentioned three | |
|
|
||
| But `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones. | ||
|
|
||
| If you don't plan meeting such scripts you may even skip this chapter or postpone it, but then there's a chance that it bites you later. | ||
| If you don't plan on reading or editing such scripts you may even skip this chapter, but there is a chance that you will see `var` in old scripts that you are reading and/or editing. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double wording: |
||
|
|
||
| From the first sight, `var` behaves similar to `let`. That is, declares a variable: | ||
|
|
||
|
|
@@ -74,9 +74,9 @@ sayHi(); | |
| alert(phrase); // Error: phrase is not defined | ||
| ``` | ||
|
|
||
| As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript blocks had no Lexical Environments. And `var` is a reminiscence of that. | ||
| As we can see, `var` pierces through `if`, `for` or other code blocks. That's because in previous versions of JavaScript, blocks had no Lexical Environments. And `var` is a reminiscence of that. | ||
|
|
||
| ## "var" are processed at the function start | ||
| ## "var" variables are processed at the start of the function | ||
|
|
||
| `var` declarations are processed when the function starts (or script starts for globals). | ||
|
|
||
|
|
@@ -126,7 +126,7 @@ function sayHi() { | |
| } | ||
| ``` | ||
|
|
||
| People also call such behavior "hoisting" (raising), because all `var` are "hoisted" (raised) to the top of the function. | ||
| People also call such behavior "hoisting" (raising), because all `var` variables are "hoisted" (raised) to the top of the function. | ||
|
|
||
| So in the example above, `if (false)` branch never executes, but that doesn't matter. The `var` inside it is processed in the beginning of the function, so at the moment of `(*)` the variable exists. | ||
|
|
||
|
|
@@ -169,16 +169,16 @@ function sayHi() { | |
| sayHi(); | ||
| ``` | ||
|
|
||
| Because all `var` declarations are processed at the function start, we can reference them at any place. But variables are undefined until the assignments. | ||
| Because all `var` declarations are processed at the function start, we can reference them at any place. But variables are undefined until they are explicitly defined. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined until they are explicitly assigned (=). |
||
|
|
||
| In both examples above `alert` runs without an error, because the variable `phrase` exists. But its value is not yet assigned, so it shows `undefined`. | ||
|
|
||
| ## Summary | ||
|
|
||
| There are two main differences of `var`: | ||
|
|
||
| 1. Variables have no block scope, they are visible minimum at the function level. | ||
| 2. Variable declarations are processed at function start. | ||
| 1. `var` Variables have no block scope, they are not block-local and can be accessed from any point in the script. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong, they are function-local, so can't be "accessed from any point in the script" =)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if rephrasing is good here...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some rephrasing is good here. In this case, I had intended to submit another PR to add precision.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
| 2. `var` Variable declarations are processed at function start. | ||
|
|
||
| There's one more minor difference related to the global object, we'll cover that in the next chapter. | ||
|
|
||
|
|
||