Function declarations cannot be nested in non-function blocks

I read about function declarations and function expressions, and I cannot understand the meaning of the following statement:

Function declarations occur as standalone constructs and cannot be nested inside non-functional blocks.

Someone please explain with an example what the author means, namely: "... cannot be embedded in non-functional blocks."

Link: https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

+5
source share
4 answers

I do not know what the author meant that this is physically impossible, or more than this should not be done. As far as I understand, the author said that this:

var y = true; if (y) { function example() { alert('hi'); return true; } } 

Here the function is declared inside the conditional operator, which is fine, since x is true, but if it was false, the function will never be declared, and when we want to call the example call, nothing will happen because it was never declared. So what it should be

 function example() { "use strict"; return true; } var y = true; if (y) { example(); } 

In the above code, we still call the example function if the condition is met, however, since the example is defined outside the condition operator, we can use it independently of the conditional operator. This post has more information about this . Hope this is what you had in mind

+2
source

Taken at face value, statement:

Function declarations occur as standalone constructs and cannot be nested inside non-functional blocks.

wrong. As examples in the article, you can place function declarations inside blocks. The reason he warns is because the behavior is different in different browsers. In most browsers (unspecified versions of IE and Firefox), such functions are declared regardless of whether execution is in the block or not, for example:

 if (false) { function foo(){} } 

foo is declared and available in the outside area. This is exactly the same as variable declarations:

 if (false) { var x = 3; } 

In the above, x is declared regardless of whether the block is running or not. However, the assignment of a value occurs only when a block is entered.

Back to features. The declaration of the function of the cause function in blocks is warned that, firstly, this means that the function is created only when the block is entered, which is not true for most browsers, but not for all. Secondly, and more importantly, because different browsers have different behavior.

Interesting reading:

Also note that function statements are warned in ES5 strict mode and may be introduced in some future version of ECMAScript.

Finally, this behavior is addressed directly in ECMA-262 ed 6 in Appendix B 3.3.3 and Appendix B 3.4 .

+2
source

I think this means that you cannot arbitrarily define functions in the code, see below.

 if true { function funName(){}; } 

funName will not be a function in this case, it will cause an error.

+1
source

Consider the modest if :

 function whatever() { // ... if (something === somethingElse) { function aFunction() { // ... } // more code ... } aFunction(5, 6, 7); 

Now this code is weird. The function is declared inside the if block. But feature declarations go up! So what does that mean?

Stranger: what if there is another declaration for "aFunction" in the else clause?

The fundamental aspect of the oddity of such code is that function declarations are treated as if they were met in the upper part of the field (that is, they were "raised"). For this reason, declaring a function inside some other block is simply inherently ambiguous and strange.

Note that executing a function using functional expressions is not strange, as they occur as part of executable code, such as an object initialization expression.

+1
source

All Articles