Take the 2-minute tour ×

Can someone explain this? IE8

( function(){
    window.foo = function foo(){};
    console.log( window.foo === foo ); // false
}() );
share|improve this question
Very good read on 'named function expressions', which is what you've got there: kangax.github.com/nfe –  InfinitiesLoop Dec 27 '11 at 18:49
add comment

2 Answers

up vote 13 down vote accepted

Due to an IE bug, the named function expression creates a separate local foo variable with a separate copy of the function.

More info:

var f = function g(){};
f === g; // false

This is where things are getting interesting. Or rather — completely nuts. Here we are seeing the dangers of having to deal with two distinct objects — augmenting one of them obviously does not modify the other one; This could be quite troublesome if you decided to employ, say, caching mechanism and store something in a property of f, then tried accessing it as a property of g, thinking that it is the same object you’re working with.

share|improve this answer
Awesome, thanks. I've read that before but have never been hit by it until now. –  zyklus Dec 27 '11 at 18:46
Once again, javascript is a mess. –  BlueRaja - Danny Pflughoeft Dec 28 '11 at 0:48
add comment

If you're interested in correcting the issue, this will work.

( function(){
    var f = function foo(){};
    window.foo = f;
    alert( window.foo === f ); // false
}() );
share|improve this answer
Or just get rid of the name –  SLaks Dec 27 '11 at 18:32
add comment

Your Answer


