Can we define “function declaration” as well as “function statement”?

This question arises with regard to function expressions. function expressions VS.

We clearly know that function declarations take this form.

function foo() { 
    var a = 3;
    console.log( a );
}

while a function expression may have this form (similar to what is known as a direct function expression being called)

(function foo() { 
    var a = 3;
    console.log( a );
})()

Looking at the expression called right away, I can just notice the first function (the one used in the function declaration) wrapped in parethesis.

Now, the thing is, I know that the grouping operator (commonly known as "parethesis" ()) can only contain an expression. If this is the case, I cannot say that declaring a function is also an expression of the function (because it would be like I enclosed an instruction in brackets ...).

, , . ? , , , ?

, . , , : " " " "?

+4
2

, .

, , . :

function foo() { 
    var a = 3;
    console.log( a );
}

foo ( , ).

:

(function foo() { 
    var a = 3;
    console.log( a );
})

foo .

, . , .

+3

hoisting , .

js . JIT .

JIT , , , . , .

IIFE - , .

callMe();  // Function statement

function callMe(){

return console.log("I am function statement");
}


// callMe2();  // TypeError if you call before expression defined.

var callMe2 = function(){
  return console.log("I am function expression");
};

callMe2(); //Function expression call

(function(){
return console.log("I am IIFE");
}());
Hide result
+1

Source: https://habr.com/ru/post/1689265/


All Articles