Does the block create a new local area inside the function area?

For example:

function example() { console.log("outside the if block above function b declaration"+b()); function a() { return "you invoked function a"; } if (true) { console.log("inside the if block"+a()); console.log("inside the if block above function b declaration"+b()); function b() { return "you invoked function b"; } } } 

When I call this function example (), I get an error that b is undefined, but when I delete the second line that calls the function b defined inside the if block. Is all this normal?

+7
javascript
source share
3 answers

Yes and no. The let keyword supports the local scope in blocks. The keywords function and var work in the function level domain. They determine the identifier when a block is compiled before execution. This way you can usually call functions on the declaration.

In your example, the function is declared conditionally. It will be declared after calculating the condition and before executing the indoor unit. But when it is declared, it is valid in the entire field of function. Try moving the invokation below the if block and it will be known and executed.

+6
source share

No, it is not. If the blocks remain in the range of their containers. Your B function is undefined because the code that defines it never runs if it is blocked by an if statement. However, as soon as the if statement is deleted, the code defining the function defining the function is executed.

Here is a jsfiddle demonstrating the concept .

 function defining() { ifstmt = true; if(ifstmt) { var ifscope; ifscope = "yes"; } console.log(ifscope); // logs "yes" } function notDefining () { ifstmt = false; if(ifstmt) { var ifscope; ifscope = "no"; } console.log(ifscope); // logs undefined } defining() notDefining() 

defining() defines the ifscope variable, but is not explicitly limited to the scope of the if statement. In notDefining() code specifying ifscope is skipped, and therefore console.log returns undefined.

+2
source share

As Michael Lumley noted in the answer, in general, a code requires that something happens before this code is executed. But Javascript support is "hoisting", which allows the encoder to call a piece of code before it is defined (more about lifting - https://developer.mozilla.org/en-US/docs/Glossary/Hoisting ) And, therefore, the following code works fine:

 example() function example() { console.log("outside the if block above function b declaration"+b()); if (true) { console.log("inside the if block"+a()); console.log("inside the if block above function b declaration"+b()); } } function a() { return "you invoked function a"; } function b() { return "you invoked function b"; } 

Here is jsfiddle - https://jsfiddle.net/px2ghrgy/

But, nevertheless, your code, apparently, will not work, because it seems conditional, does not support lifting as such. And the reason is that the region, that is, while functions rise in the enclosing region (both global and inside the function), this does not happen inside the conditional (if / else). You will also find the corresponding answer β†’ fooobar.com/questions/846015 / ...

0
source share

All Articles