From ed8560645ad2a6436f835f5f0fc6190cde3cd663 Mon Sep 17 00:00:00 2001 From: "Violet.Lee" Date: Sat, 15 Jun 2019 20:44:30 +0900 Subject: [PATCH] update example scripts --- 1-js/06-advanced-functions/04-var/article.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/1-js/06-advanced-functions/04-var/article.md b/1-js/06-advanced-functions/04-var/article.md index 0a366664..dfab568d 100644 --- a/1-js/06-advanced-functions/04-var/article.md +++ b/1-js/06-advanced-functions/04-var/article.md @@ -35,7 +35,7 @@ alert(phrase); // Error, phrase is not defined For instance: -```js +```js run if (true) { 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: -```js +```js run function sayHi() { if (true) { var phrase = "Hello"; @@ -71,7 +71,7 @@ function 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. @@ -84,7 +84,7 @@ In other words, `var` variables are defined from the beginning of the function, So this code: -```js +```js run function sayHi() { phrase = "Hello"; @@ -94,11 +94,12 @@ function sayHi() { var phrase; */!* } +sayHi(); ``` ...Is technically the same as this (moved `var phrase` above): -```js +```js run function sayHi() { *!* var phrase; @@ -108,11 +109,12 @@ function sayHi() { alert(phrase); } +sayHi(); ``` ...Or even as this (remember, code blocks are ignored): -```js +```js run function sayHi() { phrase = "Hello"; // (*) @@ -124,6 +126,7 @@ function sayHi() { alert(phrase); } +sayHi(); ``` People also call such behavior "hoisting" (raising), because all `var` are "hoisted" (raised) to the top of the function.