Obsolete since JavaScript 1.8.7
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 the scope chain for the JavaScript code currently running in a given context.
JS_GetGlobalForScopeChain
.Syntax
JSObject * JS_GetScopeChain(JSContext *cx);
Name | Type | Description |
---|---|---|
cx |
JSContext * |
The context to query. |
Description
JS_GetScopeChain
returns the first JSObject
on the scope chain for the JavaScript code currently running in the given context, cx
.
The ECMAScript standard, ECMA 262-3 §10, describes execution contexts and scope chains. A scope chain is a sequence of objects whose properties are searched whenever a script or function refers to a variable. These objects represent the lexical scope of the currently executing statement or expression, not the call stack, so they include:
- the variable objects of any enclosing functions or
let
statements or expressions, and any objects selected by enclosingwith
statements, in order from the most-nested scope outward; - lastly the global object against which the function was created.
Note that cx
's current global object, as set by JS_SetGlobalObject
, is not guaranteed to be on the scope chain. If the currently executing function was created in the scope of a different global object, then that object will be the last object in the current scope chain. If the context is currently executing a script, and not in any function, it's running in a scope that was passed to JS_ExecuteScript
or a similar function by the application. This scope chain may or may not contain cx
's current global object.
If any code is currently executing in cx
, JS_GetScopeChain
returns a pointer to the first JSObject
on the current scope chain. Otherwise, if cx
has a global object, JS_GetScopeChain
returns that. Otherwise, an error is reported and JS_GetScopeChain
returns NULL
.
To walk the scope chain, use JS_GetParent
.