Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function declarations cannot be nested within non-function blocks

I am reading about Function Declarations vs. Function Expressions, and I cannot figure out the meaning of following statement:

Function Declarations occur as standalone constructs and cannot be nested within non-function blocks.

Someone please to explain with an exemple what does the author means, precisely by: "...cannot be nested within non-function blocks".

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

like image 469
Adib Aroui Avatar asked Aug 10 '15 23:08

Adib Aroui


People also ask

What is a function declaration?

A function declaration introduces an identifier that designates a function and, optionally, specifies the types of the function parameters (the prototype). Function declarations (unlike definitions) may appear at block scope as well as file scope.

Why function declaration is placed prior to function definition?

Because otherwise the compiler will not know how to generate the code that calls the function.

Which is correct function declaration?

Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.

What is function declaration and function definition?

A function is a subprogram that returns a value. The data type of the value is the data type of the function. A function invocation (or call) is an expression, whose data type is that of the function. Before invoking a function, you must declare and define it.


2 Answers

I dont know the author meant it was physically impossible or more of it shouldn't be done. From my understanding what the author was saying is that this:

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

Here the function is declared inside a conditional statement, which is fine since x is true, but if it were false that function would never be declared and when we do want to the call the example function nothing will happen because it was never declared. So 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 example is defined outside the condition statement we can use it regardless of the conditional statement. This Post has more information about it. Hopefully this is what you meant

like image 99
code Avatar answered Nov 07 '22 16:11

code


Taken at face value, the statement:

Function Declarations occur as standalone constructs and cannot be nested within non-function blocks.

is wrong. It's possible to put function declarations inside blocks, as examples in the article show. The reason that it's warned against is that the behaviour differs in different browsers. In most browsers (not certain versions of IE and Firefox), such functions are declared regardless of whether execution enters the block or not, e.g.:

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

foo is declared and available within the outer scope. This is exactly the same with variable declarations:

if (false) {
  var x = 3;
}

In the above, x is declared regardless of whether the block is executed or not. The assignment of the value, however, only occurs if the block is entered.

Back to functions. The reason function declarations in blocks is warned against is that firstly, it infers that the function is only created if the block is entered, which is incorrect for most browsers but not all. Secondly, and more importantly, it's because different browsers have different behaviour.

Some interesting reading:

  • Richard Cornford: FunctionExpressions and memory consumption
  • Kangax: Function statements
  • What are the precise semantics of block-level functions in ES6?

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

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

like image 2
RobG Avatar answered Nov 07 '22 16:11

RobG