JS_NewArrayObject 编辑

Create a new Array object.

Syntax

JSObject *
JS_NewArrayObject(JSContext *cx, const JS::HandleValueArray& contents); // Added in SpiderMonkey 31

JSObject *
JS_NewArrayObject(JSContext *cx, size_t length); // Added in SpiderMonkey 31

JSObject *
JS_NewArrayObject(JSContext *cx, int length, jsval *vector); // Obsolete since JSAPI 30
NameTypeDescription
cxJSContext *The context in which to create the new array. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
contentsJS::HandleValueArray&Reference to the initial values for the array's elements. Added in SpiderMonkey 31
lengthsize_t or intThe length of the new array. This must not be negative.
vectorjsval *Pointer to the initial values for the array's elements, or NULL. Obsolete since JSAPI 30

Description

JS_NewArrayObject with contents parameter creates a new array object with the specified contents elements. JS_NewArrayObject defines an enumerable array element with the value contents[i] on the new array.

JS_NewArrayObject with length parameter creates a new array object with the specified length; the result is like the JavaScript expression new Array(length). The new array has the specified length, but it doesn't have any elements.

On success, JS_NewArrayObject returns the new array object. Otherwise it reports an error as though by calling JS_ReportOutOfMemory and returns NULL.

Obsolete since JSAPI 30.

JS_NewArrayObject with length parameter creates a new array object with the specified length. If vector is non-null, then for each index i from 0 to length - 1, JS_NewArrayObject defines an enumerable array element with the value vector[i] on the new array. (This means that if length is nonzero and vector is null, the result is like the JavaScript expression new Array(length). The new array has the specified length, but it doesn't have any elements.)

It is often better to call JS_NewArrayObject(cx, 0, NULL), store the returned object in a GC root, and then populate its elements with JS_SetElement or JS_DefineElement. This avoids unrooted jsvals in vector from being subject to garbage collection until the new object has been populated.

See Also

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

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

发布评论

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

词条统计

浏览:126 次

字数:4870

最后编辑:6年前

编辑次数:0 次

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