From 46a9bb5e34a1c0003e1c2367599c2f4dae969c7a Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Mon, 1 Jul 2019 13:59:53 +0300 Subject: [PATCH] use strict for FD visibility --- .../15-function-expressions-arrows/article.md | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/1-js/02-first-steps/15-function-expressions-arrows/article.md b/1-js/02-first-steps/15-function-expressions-arrows/article.md index 9b63907d..a31cb4b2 100644 --- a/1-js/02-first-steps/15-function-expressions-arrows/article.md +++ b/1-js/02-first-steps/15-function-expressions-arrows/article.md @@ -196,19 +196,20 @@ First, the syntax: how to differentiate between them in the code. The more subtle difference is *when* a function is created by the JavaScript engine. -**A Function Expression is created when the execution reaches it and is usable from then on.** +**A Function Expression is created when the execution reaches it and is usable only from that moment.** Once the execution flow passes to the right side of the assignment `let sum = function…` -- here we go, the function is created and can be used (assigned, called, etc. ) from now on. Function Declarations are different. -**A Function Declaration is usable in the whole script (or a code block, if it's inside a block).** +**A Function Declaration can be called earlier than it is defined.** -In other words, when JavaScript *prepares* to run the script or a code block, it first looks for Function Declarations in it and creates the functions. We can think of it as an "initialization stage". +For example, a global Function Declaration is visible in the whole script, no matter where it is. -And after all of the Function Declarations are processed, the execution goes on. +That's due to internal algorithms. When JavaScript prepares to run the script, it first looks for global Function Declarations in it and creates the functions. We can think of it as an "initialization stage". + +And after all Function Declarations are processed, the code is executed. So it has access to these functions. -As a result, a function declared as a Function Declaration can be called earlier than it is defined. For example, this works: @@ -224,7 +225,7 @@ function sayHi(name) { The Function Declaration `sayHi` is created when JavaScript is preparing to start the script and is visible everywhere in it. -...If it was a Function Expression, then it wouldn't work: +...If it were a Function Expression, then it wouldn't work: ```js run refresh untrusted *!* @@ -238,13 +239,11 @@ let sayHi = function(name) { // (*) no magic any more Function Expressions are created when the execution reaches them. That would happen only in the line `(*)`. Too late. -**When a Function Declaration is made within a code block, it is visible everywhere inside that block. But not outside of it.** - -Sometimes that's handy to declare a local function only needed in that block alone. But that feature may also cause problems. +**In strict mode, when a Function Declaration is within a code block, it's visible everywhere inside that block. But not outside of it.** For instance, let's imagine that we need to declare a function `welcome()` depending on the `age` variable that we get during runtime. And then we plan to use it some time later. -The code below doesn't work: +If we use Function Declaration, it won't work as intended: ```js run let age = prompt("What is your age?", 18); @@ -350,12 +349,12 @@ welcome(); // ok now ``` -```smart header="When should you choose Function Declaration versus Function Expression?" -As a rule of thumb, when we need to declare a function, the first to consider is Function Declaration syntax, the one we used before. It gives more freedom in how to organize our code, because we can call such functions before they are declared. +```smart header="When to choose Function Declaration versus Function Expression?" +As a rule of thumb, when we need to declare a function, the first to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared. -It's also a little bit easier to look up `function f(…) {…}` in the code than `let f = function(…) {…}`. Function Declarations are more "eye-catching". +That's also better for readability, as it's easier to look up `function f(…) {…}` in the code than `let f = function(…) {…}`. Function Declarations are more "eye-catching". -...But if a Function Declaration does not suit us for some reason (we've seen an example above), then Function Expression should be used. +...But if a Function Declaration does not suit us for some reason, or we need a conditional declaration (we've just seen an example), then Function Expression should be used. ```