global object. In browsers. the
window object doubles as the
global object is something else. Most of the time, it’s not assigned to a global variable for you to access.
window for dealing with globals. However, referencing the
global object can be necessary. To that end, I present the
The key to this function is that the
this object always points to the
global object anytime you are using
apply() and pass in
null as the first argument. Since a
null scope is not valid, the interpreter inserts the
global object. The function uses an inner function to assure that the scope is always correct. You can then use this function as follows:
var global = getGlobal();
Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.
Both comments and pings are currently closed.