This is an archived page. It's not actively maintained.


This method is used to determine the global object with which an object is associated. This is the global object in place at the time the object was created, which is to say the global object used when executing the script that created the object.


var global = Components.utils.getGlobalForObject(obj);


an object whose corresponding global object is to be retrieved; non-optional, must be object-valued


var obj = {};
function foo() { }
var global = this;

var g1 = Components.utils.getGlobalForObject(foo);
var g2 = Components.utils.getGlobalForObject(obj);
// g1 === global, g2 === global, g1 === g2

// In a script in another window
var global2 = this;
function bar() { }
var obj2 = {};

// Then, assuming bar refers to the function defined in that other window:
var o1 = Components.utils.getGlobalForObject(bar);
var o2 = Components.utils.getGlobalForObject(obj2);
// o1 === global2, o2 === global2