Obsolete since JSAPI 39
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.

Get the parent object of a given object.


JSObject *
JS_GetParent(JSObject *obj);
Name Type Description
obj JSObject * Object for which to retrieve the parent.


JS_GetParent retrieves the parent object of obj, or NULL if obj does not have a parent.

Warning: The result may be an internal-use-only object. Such an object should not be passed to any other JSAPI function that could expose it to script code.

Each object is assigned a parent when it is created:

The preferred way to set an object's parent is by using the parent parameter to JS_NewObject or JS_ConstructObject.  An application can also use JS_SetParent, but see the caveats on that page.

An object's parent serves two purposes in SpiderMonkey:

  1. For some functions, it is used to implement lexical scoping (but this is an implementation detail).
  2. Applications that use SpiderMonkey's custom security features can use the parent during security checks, as a convenient way to determine the security domain of an object being accessed. The typical rule is that every object is in the same security domain as its parent. (But each application can define its own security rules. See the Security section of the User Guide for an introduction to the security model.)

In some cases, JavaScript code can get an object's parent via the read-only obj.__parent__ property. But if an object's parent is an internal-use-only object (that is, a special Call, With, or Block object), JS_GetParent simply returns it, but obj.__parent__ === null. Similarly, if an object's parent is an inner object, JS_GetParent simply returns it, but obj.__parent__ returns the corresponding outer object.

See Also