-1

var fn = function example(){
 console.error('Hello');
}
  1. I want to understand how function initialization with name 'example' doesn't throw error during execution.
  2. Secondly i understand the flow of 'fn' holds the reference of the function i'm assigning to it, were when i execute 'fn()' it works, were as when i try 'example()' it doesn't print "Hello" Help me to know how that stuff works !!
Sasi Dunston
  • 302
  • 1
  • 4
  • 15
  • 1
    Possible duplicate of [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – vibhor1997a Jul 13 '18 at 07:49

1 Answers1

1
  1. Because you assign only name example to anonymous function. You do not create actual example function.

  2. Your fn holds function that has it's name as example

var fn = function example() {
 console.error('Hello');
}

var fn2 = function () {
 console.error('Hello 2');
}

function example2() {
  console.error('Hello 3');
}

console.log(fn.name);
console.log(fn2.name);
console.log(example2.name);
console.log("");
console.log(window['example']);
console.log(window['fn']);
console.log(window['fn2']);
console.log(window['example2']);
Justinas
  • 41,402
  • 5
  • 66
  • 96