From 99caa80018950f030bb82df8ee7738cbd224e21f Mon Sep 17 00:00:00 2001 From: MuhammedZakir Date: Fri, 11 Sep 2020 13:31:57 +0530 Subject: [PATCH 1/2] Fix javascript-tutorial#2098 - replace let with var in IIFE example --- 1-js/06-advanced-functions/04-var/article.md | 26 +++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/1-js/06-advanced-functions/04-var/article.md b/1-js/06-advanced-functions/04-var/article.md index 1e8bb5baee..400671161e 100644 --- a/1-js/06-advanced-functions/04-var/article.md +++ b/1-js/06-advanced-functions/04-var/article.md @@ -28,7 +28,7 @@ On the other hand, it's important to understand differences when migrating old s ## "var" has no block scope -Variables, declared with `var`, are either function-wide or global. They are visible through blocks. +Variables, declared with `var`, are either function-scoped or global-scoped. They are visible through blocks. For instance: @@ -60,11 +60,13 @@ The same thing for loops: `var` cannot be block- or loop-local: ```js for (var i = 0; i < 10; i++) { + var one = 1; // ... } *!* -alert(i); // 10, "i" is visible after loop, it's a global variable +alert(i); // 10, "i" is visible after loop, it's a global variable +alert(one); // 1, "one" is visible after loop, it's a global variable */!* ``` @@ -83,7 +85,7 @@ sayHi(); 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. ## "var" tolerates redeclarations @@ -203,11 +205,11 @@ 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. -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`. +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`. -### IIFE +## IIFE -As in the past there was only `var`, and it has no block-level visibility, programmers invented a way to emulate it. What they did was called "immediately-invoked function expressions" (abbreviated as IIFE). +In the past, as there was only `var`, and it has no block-level visibility, programmers invented a way to emulate it. What they did was called "immediately-invoked function expressions" (abbreviated as IIFE). That's not something we should use nowadays, but you can find them in old scripts. @@ -216,22 +218,22 @@ An IIFE looks like this: ```js run (function() { - let message = "Hello"; + var message = "Hello"; alert(message); // Hello })(); ``` -Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables. +Here, a Function Expression is created and immediately called. So the code executes right away and has its own private variables. -The Function Expression is wrapped with parenthesis `(function {...})`, because when JavaScript meets `"function"` in the main code flow, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so this kind of code will give an error: +The Function Expression is wrapped with parenthesis `(function {...})`, because when JavaScript engine encounters `"function"` in the main code, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so this kind of code will give an error: ```js run -// Try to declare and immediately call a function +// Tries to declare and immediately call a function function() { // <-- Error: Function statements require a function name - let message = "Hello"; + var message = "Hello"; alert(message); // Hello @@ -277,7 +279,7 @@ In all the above cases we declare a Function Expression and run it immediately. There are two main differences of `var` compared to `let/const`: -1. `var` variables have no block scope, they are visible minimum at the function level. +1. `var` variables have no block scope, they are visibility is scoped to current function, or global, if declared outside function. 2. `var` declarations are processed at function start (script start for globals). There's one more very minor difference related to the global object, that we'll cover in the next chapter. From c040a90672297d55c5c0436b37e27c05c763a8a7 Mon Sep 17 00:00:00 2001 From: Muhammed Zakir <8190126+MuhammedZakir@users.noreply.github.com> Date: Fri, 11 Sep 2020 10:00:53 +0000 Subject: [PATCH 2/2] Added requested change --- 1-js/06-advanced-functions/04-var/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/06-advanced-functions/04-var/article.md b/1-js/06-advanced-functions/04-var/article.md index 400671161e..b2559989bd 100644 --- a/1-js/06-advanced-functions/04-var/article.md +++ b/1-js/06-advanced-functions/04-var/article.md @@ -279,7 +279,7 @@ In all the above cases we declare a Function Expression and run it immediately. There are two main differences of `var` compared to `let/const`: -1. `var` variables have no block scope, they are visibility is scoped to current function, or global, if declared outside function. +1. `var` variables have no block scope; their visibility is scoped to current function, or global, if declared outside function. 2. `var` declarations are processed at function start (script start for globals). There's one more very minor difference related to the global object, that we'll cover in the next chapter.