JSNewEnumerateOp 编辑

The type of ObjectOps::enumerate. This callback overrides a portion of SpiderMonkey's default [[Enumerate]] internal method.

Syntax

typedef bool
(* JSNewEnumerateOp)(JSContext *cx, JS::HandleObject obj,
                     JS::AutoIdVector &properties); // Added in SpiderMonkeySidebar 38

typedef bool
(* JSNewEnumerateOp)(JSContext *cx, JS::HandleObject obj, JSIterateOp enum_op,
                     JS::MutableHandleValue statep, JS::MutableHandleId idp); // Obsolete since JSAPI 37
NameTypeDescription
cxJSContext *The context in which the enumeration is taking place.
objJS::HandleObjectThe object to be enumerated.
propertiesJS::AutoIdVector &Out parameter. Id array to populate with all property keys.
enum_opJSIterateOpObsolete since JSAPI 37 Specifies which step in iteration is happening. See the Description below.
statepJS::MutableHandleIdObsolete since JSAPI 37 In/out parameter. The meaning depends on enum_op. See the Description below.
idpJS::MutableHandleIdObsolete since JSAPI 37 In/out parameter. The meaning depends on enum_op. See the Description below.

Description

From SpiderMonkey 38, JSNewEnumerateOp is the type of ObjectOps::enumerate. This callback overrides a portion of SpiderMonkey's default [[Enumerate]] internal method. When an ordinary object is enumerated, that object and each object on its prototype chain is tested for an enumerate op, and those ops are called in order. The properties each op adds to the properties vector are added to the set of values the for-in loop will iterate over. All of this is nonstandard.

An object is "enumerated" when it's the target of a for-in loop or JS_Enumerate. All other property inspection, including Object.keys(obj), goes through [[OwnKeys]].

The callback's job is to populate properties with all property keys that the for-in loop should visit.

Before JSAPI 37, JSNewEnumerateOp was the type of JSClass.enumerate.

To use a JSNewEnumerateOp in a JSClass, set the JSCLASS_NEW_ENUMERATE bit in the JSClass.flags field and set the JSClass.enumerate field to your JSNewEnumerateOp, casting it to JSEnumerateOp. (SpiderMonkey, noting the JSCLASS_NEW_ENUMERATE flag, will cast that function pointer back to type JSNewEnumerateOp before calling it.)

The behavior depends on the value of enum_op:

JSENUMERATE_INIT

A new, opaque iterator state should be allocated and stored in *statep. (You can use PRIVATE_TO_JSVAL to tag the pointer to be stored).

The number of properties that will be enumerated should be returned as an integer jsval in *idp, if idp is non-null, and provided the number of enumerable properties is known. If idp is non-null and the number of enumerable properties can't be computed in advance, *idp should be set to JSVAL_ZERO.

JSENUMERATE_INIT_ALL
Used identically to JSENUMERATE_INIT, but exposes all properties of the object regardless of enumerability.
JSENUMERATE_NEXT

A previously allocated opaque iterator state is passed in via statep. Return the next jsid in the iteration using *idp. The opaque iterator state pointed at by statep is destroyed and *statep is set to JSVAL_NULL if there are no properties left to enumerate.

JSENUMERATE_DESTROY

Destroy the opaque iterator state previously allocated in *statep by a call to this function when enum_op was JSENUMERATE_INIT or JSENUMERATE_INIT_ALL.

The return value is used to indicate success, with a value of false indicating failure.

See Also

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

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

发布评论

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

词条统计

浏览:66 次

字数:7784

最后编辑:8年前

编辑次数:0 次

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