JS_GetGlobalObject 编辑

Obsolete since JSAPI 24
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 a context's global object. (In JavaScript, global variables are stored as properties of the global object.)

Syntax

JSObject *
JS_GetGlobalObject(JSContext *cx);
NameTypeDescription
cxJSContext *The context from which to retrieve the global object.

Description

This function is obsolete: use JS_GetGlobalForObject or JS_GetGlobalForScopeChain instead. The concept of a global object belonging to a context will likely be phased out in future versions of SpiderMonkey.

JS_GetGlobalObject retrieves the global object for a specified JSContext *, cx. This returns NULL if cx has no global object.

SpiderMonkey supports applications that have multiple global objects, such as the various window objects in a Web browser. The object returned by JS_GetGlobalObject is not necessarily the same thing as the global object seen by any JavaScript code that runs in cx! If a function is executing, the global object used by that function is determined when the function is created. Furthermore, some JSAPI functions, such as JS_ExecuteScript, allow the caller to specify a global object in which the script executes. In either case, the global function used by the function or script may differ from the context's global object.

The context's global object field serves two purposes. First, it is a convenient place for the application to stash the global object, where it is automatically protected from garbage collection and the application can easily access it. Second, the context's global object is used as a default value of last resort by functions that need a default parent object (see JS_SetParent for details) and by JS_GetScopeChain.

Use JS_SetGlobalObject to replace the global object.

someone should document common use case "giving the global object a name", which can be done with JS_GetGlobalObject and JS_SetProperty

See Also

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

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

发布评论

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

词条统计

浏览:144 次

字数:5199

最后编辑:8年前

编辑次数:0 次

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