Declared functions vs Expressed functions

0

I have a question about this:

According to what I have read, the declared functions are available for your call regardless of their position in the code.

Even if the declaration is at the end of a whole source code, it will take precedence over those expressions that precede it.

However, if we deal with expressed functions, they are only evaluated when the natural flow of execution reaches them.

In ECMASCRIPT6 this is still literally like this? Because in the following example, the web where I found the information describes:

  

Since the function is created before the code is evaluated, in some   browsers we can find that we skip the   conditional and the foo function is always assigned the value FALSE, which   it would correspond with the last call made.

myVar=true;

if( myVar == true){
  function foo(){ return 'TRUE'; }
}else{
  function foo(){ return 'FALSE'; }
}

console.log(foo());

When I try the above, contrary to what is said, the result is TRUE.

Do you really have to find if you create a function in one way or another, or on the contrary you choose mostly for the declaration (as in almost all projects I find)?

    
asked by Mario G. Lucas 07.08.2018 в 21:39
source

2 answers

3

About the preamble of the question

An expressed function could have the following form

//Ejemplo de función expresada
var miFuncion = function(){
   //Aquí van los enunciados de lo que hará la función expresada
}

Another example of a function expressed is

//Ejemplos de función autoejecutada o IIFE por las siglas de su nombre en inglés
(function miOtraFuncion(){})()

The example included in the question does not contain expressed functions. The functions included in the question are "declared" functions.

A function is "declared" when the statement starts with the keyword function

A function is "expressed" when an expression is a function. An expression is a unit that returns a value.

About the question at the end of the body of the question

  

Do you really have to find (sic) if you create a function in one way or another, or on the contrary you choose mostly for the declaration (as in almost all projects I find)?

Definitely you have to take into account the way to create functions but first of all this dictates what you want the program to do, then you will consider maintenance ease objectives, "good practices", style guides and preferences. of the programmer.

Reference

answered by 07.08.2018 в 22:26
0

Everything will depend if:

  • strict mode is activated
  • the interpreter is ES6

For implementations ES<6 the functions can not be declared inside blocks of control structures, this is an undefended behavior if the stricto mode is activated, this will throw an exception, otherwise the result will vary according to the implementation

the majority of browsers, when this problem is encountered, perform the isado

if (true) {
  function foo () { return true };
} else {
  function foo () { return false }; 
}
console.log(foo()); // false

is equivalent to

function foo () { return true };
function foo () { return false };

if (true) {
  ; //no-op
} else {
  ; //no-op
}
console.log(foo()); // false

In stricto mode this will throw an exception

For implementations ES>=6 function definitions will inherit the scope of the block, and expressions will be evaluated allowing you to change the definition of the function and even not export it (as opposed to the isado)

like this:

if (true) {
  function foo () { return true };
} else {
  function foo () { return false };
}
console.log(foo());

is approximately the same

{
  var foo;
  if (true) {
    foo = function foo () { return true };
  } else {
    foo = function foo () { return false };
  }
  if (foo !== undefined) window.foo = foo // window porque es el scope global
}
console.log(foo());

link

    
answered by 08.08.2018 в 14:43