use strict for FD visibility
This commit is contained in:
parent
27a6d60593
commit
46a9bb5e34
1 changed files with 13 additions and 14 deletions
|
@ -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.
|
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.
|
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.
|
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:
|
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.
|
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
|
```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.
|
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.**
|
**In strict mode, when a Function Declaration is within a code block, it's 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.
|
|
||||||
|
|
||||||
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.
|
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
|
```js run
|
||||||
let age = prompt("What is your age?", 18);
|
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?"
|
```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, 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.
|
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.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue