Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function references - Please enlighten!

Tags:

javascript

Could anyone explain why the two below are not equal? I'm basically trying to figure out what's happening behind the scenes. My understanding was that they'd both refer to the same function but that doesn't seem to be the case.

var foo = function bar() {}

typeof foo //"function"
typeof bar //"function"

foo === bar //false
foo == bar //false
like image 614
John Strickler Avatar asked Jun 14 '11 15:06

John Strickler


2 Answers

I don't know about you but my browsers return undefined for typeof bar

typeof bar //"undefined"

demo http://jsfiddle.net/gaby/t8Czr/


The bar reference to the method is only available inside the method itself..

like image 99
Gabriele Petrioli Avatar answered Sep 20 '22 20:09

Gabriele Petrioli


Bar is not defined. Bar only exists within the scope of foo.

If you were to declare bar first then set foo equal to bar your results will be as expected.

like image 43
Jrod Avatar answered Sep 20 '22 20:09

Jrod