Visit Mozilla.org

JS GetScopeChain

From MDC

Retrieves the scope chain for the JavaScript code currently running in a given context.

[edit] Syntax

JSObject * JS_GetScopeChain(JSContext *cx);
Name Type Description
cx JSContext * The context to query.

[edit] 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:

  • first, the variable object that contains the local variables of the currently executing function;
  • then, the variable objects of any enclosing functions or let statements or expressions, and any objects selected by enclosing with statements, in order from the most-nested scope outward;
  • and lastly the the global object.

Note that cx's current global object 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 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.

LXR ID Search for JS_GetScopeChain