JS_SetParent 编辑

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.

Sets the parent for an object.

Syntax

bool
JS_SetParent(JSContext *cx, JS::HandleObject obj, JS::HandleObject parent);
NameTypeDescription
cxJSContext *Pointer to a JS context from which to derive runtime information. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
objJS::HandleObjectPointer to the object for which to set the parent. This must be an object that has not yet been exposed to script. See the Description for details.
parentJS::HandleObjectPointer to the parent object to use.

Description

JS_SetParent sets an object's parent.

Each object may have at most one parent, which is another object. Applications that use SpiderMonkey's security features typically use the parent relation to determine both (a) what security principals are attached to the currently executing script; and (b) what security principals are attached to the object being accessed. (For some function objects, the parent chain is also used to implement lexical scoping, but this should be considered an implementation detail.)

Ordinarily an application sets a new object's parent by passing the parent object to JS_NewObject, and that is the preferred approach. But an application may instead use JS_SetParent after the object is created.

Applications can get an object's parent using JS_GetParent. Scripts can determine an object's parent by using the __parent__ property, but scripts cannot assign to the __parent__ property. In fact, once an object is exposed to a script, the object's parent must not change. The JavaScript engine relies on this invariant. JS_SetParent has no way to check that this is the case, but nonetheless, applications must not call JS_SetParent on an object that has already been exposed to a script. If an application does this, the behavior is undefined.

On success, JS_SetParent returns JS_TRUE. Otherwise, it reports an error or sets an exception and returns JS_FALSE.

See Also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:77 次

字数:4279

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文