update example scripts
This commit is contained in:
parent
f418ab35e9
commit
ed8560645a
1 changed files with 9 additions and 6 deletions
|
@ -35,7 +35,7 @@ alert(phrase); // Error, phrase is not defined
|
||||||
|
|
||||||
For instance:
|
For instance:
|
||||||
|
|
||||||
```js
|
```js run
|
||||||
if (true) {
|
if (true) {
|
||||||
var test = true; // use "var" instead of "let"
|
var test = true; // use "var" instead of "let"
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ alert(i); // 10, "i" is visible after loop, it's a global variable
|
||||||
|
|
||||||
If a code block is inside a function, then `var` becomes a function-level variable:
|
If a code block is inside a function, then `var` becomes a function-level variable:
|
||||||
|
|
||||||
```js
|
```js run
|
||||||
function sayHi() {
|
function sayHi() {
|
||||||
if (true) {
|
if (true) {
|
||||||
var phrase = "Hello";
|
var phrase = "Hello";
|
||||||
|
@ -71,7 +71,7 @@ function sayHi() {
|
||||||
}
|
}
|
||||||
|
|
||||||
sayHi();
|
sayHi();
|
||||||
alert(phrase); // Error: phrase is not defined
|
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
|
||||||
```
|
```
|
||||||
|
|
||||||
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 remnant of that.
|
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 remnant of that.
|
||||||
|
@ -84,7 +84,7 @@ In other words, `var` variables are defined from the beginning of the function,
|
||||||
|
|
||||||
So this code:
|
So this code:
|
||||||
|
|
||||||
```js
|
```js run
|
||||||
function sayHi() {
|
function sayHi() {
|
||||||
phrase = "Hello";
|
phrase = "Hello";
|
||||||
|
|
||||||
|
@ -94,11 +94,12 @@ function sayHi() {
|
||||||
var phrase;
|
var phrase;
|
||||||
*/!*
|
*/!*
|
||||||
}
|
}
|
||||||
|
sayHi();
|
||||||
```
|
```
|
||||||
|
|
||||||
...Is technically the same as this (moved `var phrase` above):
|
...Is technically the same as this (moved `var phrase` above):
|
||||||
|
|
||||||
```js
|
```js run
|
||||||
function sayHi() {
|
function sayHi() {
|
||||||
*!*
|
*!*
|
||||||
var phrase;
|
var phrase;
|
||||||
|
@ -108,11 +109,12 @@ function sayHi() {
|
||||||
|
|
||||||
alert(phrase);
|
alert(phrase);
|
||||||
}
|
}
|
||||||
|
sayHi();
|
||||||
```
|
```
|
||||||
|
|
||||||
...Or even as this (remember, code blocks are ignored):
|
...Or even as this (remember, code blocks are ignored):
|
||||||
|
|
||||||
```js
|
```js run
|
||||||
function sayHi() {
|
function sayHi() {
|
||||||
phrase = "Hello"; // (*)
|
phrase = "Hello"; // (*)
|
||||||
|
|
||||||
|
@ -124,6 +126,7 @@ function sayHi() {
|
||||||
|
|
||||||
alert(phrase);
|
alert(phrase);
|
||||||
}
|
}
|
||||||
|
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` are "hoisted" (raised) to the top of the function.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue