views:

37

answers:

1

OK, simple:

self = $(this);

Throws a JavaScript error in IE8 when it's inside an event handler. It works in every other browser.

var self = $(this);

Throws no error. Why?

+5  A: 

The answer is that var keyword staring back out at you.

When you reference just plain self, you're referencing a global variable and IE won't let you change it. When you write var self you're declaring a local variable.

Pointy
Is 'self' a reserved word in IE8, or is IE8 just weirder than every other browser regarding global JavaScript variables?
Wells
It's not a reserved word, exactly, but it's a property of the `window` object that IE won't let you alter.
Pointy