JS_GetParent 编辑
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.
Syntax
JSObject *
JS_GetParent(JSObject *obj);
Name | Type | Description |
---|---|---|
obj | JSObject * | Object for which to retrieve the parent. |
Description
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 standard library objects and functions all have the global object as their parent.
- Objects created by standard library functions, such as
Array.prototype.concat
, have the global object as their parent. - An object created by a script via the
new
keyword has the same parent as the constructor. For example, afterd = new Date
,d
has the same parent asDate
, ordinarily the global object. - An object created by a script via implicit conversion (the ECMAScript ToObject operator), an object initializer (
{a: 1, b: 2}
), or an array initializer ([1, 2, 3]
), has the global object as its parent. - The parent of a
Function
created by evaluating a function declaration or function expression in a script depends on the lexical scope of the function. If there is no enclosing function,with
statement, or block scope, then theFunction
's parent is the global object. Otherwise, the parent is implementation-defined. It may be a Call, With, or Block object representing the function's dynamic scope, or it may be the global object. In any case the global object is guaranteed appear on the new function's parent chain. The initial parent of an object created via the JSAPI depends on the particular JSAPI function (of which there are many that create objects, including but not limited to
JS_NewObject
,JS_ConstructObject
,JS_DefineObject
,JS_ValueToObject
,JS_NewArrayObject
,JS_NewScriptObject
,JS_NewFunction
,JS_CompileFunction
,JS_DefineFunction
,JS_DefineFunctions
, andJS_InitClass
). Some of these functions allow the application to specify a parent object. If the JSAPI function creating the object has aparent
parameter, and the application passes a non-null value to it, then that object becomes the new object's parent. Otherwise, if the context is running any scripts or functions, a default parent object is selected based on those. Otherwise, if the context has a global object, it is used. Otherwise the new object has no parent. This is often the case for the global object, which is typically the first object created in a new context.
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:
- For some functions, it is used to implement lexical scoping (but this is an implementation detail).
- 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
- MXR ID Search for
JS_GetParent
- bug 1131805 -- removed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论