JS_ConstructObject 编辑

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

As of SpiderMonkey 1.8.8, JS_ConstructObject and JS_ConstructObjectWithArguments have been removed from the JSAPI.  The preferred alternative is to save a copy of the constructor function for the class, then to call it using JS_New.  A less-preferred short-term solution might be to use this reimplementation of the method, but note that this reimplementation is not guaranteed to continue working across SpiderMonkey releases.

Create a new object of the specified class, with the specified prototype and parent, then invokes a constructor function to initialize the new object.

Syntax

JSObject *
JS_ConstructObject(JSContext *cx, JSClass *clasp,
    JSObject *proto, JSObject *parent);

JSObject *
JS_ConstructObjectWithArguments(JSContext *cx, JSClass *clasp,
    JSObject *proto, JSObject *parent, unsigned int argc, jsval *argv);
NameTypeDescription
cxJSContext *The context in which to create the new object. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
claspJSClass *Pointer to the class to use for the new object. If this is NULL, an ordinary JavaScript Object is created.
protoJSObject *The object to serve as the new object's prototype, or NULL.
parentJSObject *The object to serve as the new object's parent, or NULL.
argcunsigned int(only in JS_ConstructObjectWithArguments) The number of arguments to pass to the constructor.
argvjsval *(only in JS_ConstructObjectWithArguments) The array of arguments to pass to the constructor. This may be null if argc is zero. Otherwise, the first argc elements of this array must be populated with valid jsval values. The caller does not need to ensure that the array elements are rooted.

Description

JS_ConstructObject creates a new object of the specified class, with the specified prototype and parent, then invokes a constructor function to initialize the new object. cx is a pointer to a context associated with the runtime in which to create the new object. clasp is a pointer to the class of object to create. proto and parent may specify objects to be used as the new object's prototype and parent. If either is NULL, the engine tries to find reasonable defaults.

JS_ConstructObjectWithArguments is the same but additionally passes zero or more arguments to the constructor.

Neither of these functions is quite like the JavaScript new keyword.

For details on how we find the appropriate constructor and default prototype, see JS_NewObject: Choosing a Default Prototype.  Roughly speaking, we use parent or cx to find a global object, and then find clasp's constructor in that global object.

If parent is NULL, the engine chooses an object to serve as the new object's parent.

On success, JS_ConstructObject returns a pointer to the newly instantiated object. Otherwise it returns NULL.

The new object created by these functions is subject to garbage collection. The object should be made GC-reachable immediately. See the JSAPI User Guide.

See Also

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

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

发布评论

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

词条统计

浏览:74 次

字数:6745

最后编辑:7年前

编辑次数:0 次

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