inicio mail me! sindicaci;ón

Javascript function names

Javascript allows naming and assigning functions at the same time like:

var vname = function fname() {}

The function name fname is available only inside the function as a local variable:

var vname = function fname(){
  console.log(typeof vname);  // function
  console.log(typeof fname);  // function
}
console.log(typeof vname);    // function
console.log(typeof fname);    // undefined

If we „redefine“ this local variable inside of the function, we get a strange effect:

var vname = function fname(){
  console.log(typeof vname);  // function
  console.log(typeof fname);  // undefined !!!
  var fname = 1;
  console.log(typeof fname);  // number
}
console.log(typeof vname);    // function
console.log(typeof fname);    // undefined

Obviously the interpreter sees the variable declaration var fname on entrance into the function and does not provide the function variable at all.

Schreibe einen Kommentar