JS_GetGlobalObject

Obsolete since JSAPI 24
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

Retrieves a context's global object. (In JavaScript, global variables are stored as properties of the global object.)

Syntax

JSObject *
JS_GetGlobalObject(JSContext *cx);
Name Type Description
cx JSContext * The context from which to retrieve the global object.

Description

This function is obsolete: use JS_GetGlobalForObject or JS_GetGlobalForScopeChain instead. The concept of a global object belonging to a context will likely be phased out in future versions of SpiderMonkey.

JS_GetGlobalObject retrieves the global object for a specified JSContext *, cx. This returns NULL if cx has no global object.

SpiderMonkey supports applications that have multiple global objects, such as the various window objects in a Web browser. The object returned by JS_GetGlobalObject is not necessarily the same thing as the global object seen by any JavaScript code that runs in cx! If a function is executing, the global object used by that function is determined when the function is created. Furthermore, some JSAPI functions, such as JS_ExecuteScript, allow the caller to specify a global object in which the script executes. In either case, the global function used by the function or script may differ from the context's global object.

The context's global object field serves two purposes. First, it is a convenient place for the application to stash the global object, where it is automatically protected from garbage collection and the application can easily access it. Second, the context's global object is used as a default value of last resort by functions that need a default parent object (see JS_SetParent for details) and by JS_GetScopeChain.

Use JS_SetGlobalObject to replace the global object.

someone should document common use case "giving the global object a name", which can be done with JS_GetGlobalObject and JS_SetProperty

See Also