Most Read This Week
For more standards-respecting browsers, the way to do this should be to use the call function
By: Jim Driscoll
Sep. 8, 2009 07:15 PM
For our first naive implementation, we'd simply used
So, when we said
For more standards-respecting browsers, the way to do this should be to use the call function, which is a standard function attached to every Function object. So,
At this point, I thought we'd licked the problem. No such luck. Sure, global variables are getting set, but it turns out that if you say: alert(this) - then you would correctly receive the global object back on Chrome and Safari, but not Firefox - there, you'd get back the object that was the enclosing object before the call function got called. Very odd, and likely a bug in their implementation.
With a little help from Werner Punz, we figured out that they best way to get around this issue is to wrap the calling function in an anonymous globally scoped function. Like the Chrome bug, I can guess why this might work, but it would only be a guess. Better not to clutter up the internets with more guesses - I'll just stick to what I know works.
Here's the code that I now use to do a global eval:
Subscribe to the World's Most Powerful Newsletters
Today's Top Reads