JS_AlreadyHasOwnProperty 编辑

This article covers features introduced in SpiderMonkey 1.8

Determine whether a property is already physically present on a JSObject.

Syntax

boo
JS_AlreadyHasOwnProperty(JSContext *cx, JS::HandleObject obj, const char *name,
                         bool *foundp);

boo
JS_AlreadyHasOwnUCProperty(JSContext *cx, JS::HandleObject obj, const char16_t *name,
                           size_t namelen, bool *foundp);

boo
JS_AlreadyHasOwnPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
                             bool *foundp); // Added in SpiderMonkey 1.8.1

boo
JS_AlreadyHasOwnElement(JSContext *cx, JS::HandleObject obj, uint32_t index,
                        bool *foundp);
NameTypeDescription
cxJSContext *Pointer to a JS context. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
objJSObject *The object to be searched.
name or idconst char * or const char16_t *or JS::HandleId(in JS_AlreadyHasOwnProperty, AlreadyHasOwnUCProperty, and JS_AlreadyHasOwnPropertyById) The name of the property to search for.
namelensize_t(in JS_AlreadyHasOwnUCProperty only) The length of name, in characters; or the special value (size_t) -1 to indicate that name is null-terminated.
indexuint32_t(in JS_AlreadyHasOwnElement only) The index of the element to search for.
foundpbool *Out parameter. *foundp receives true if the property is found or false if it is not found. If an error occurs, the value left in *foundp is undefined.

Description

These functions attempt to determine whether a property already exists on a specific JSObject without modifying the object. By design, this search may not find a property that other property lookup functions, such as JS_LookupProperty, would find.

For native objects—objects whose properties are stored in the default data structure provided by SpiderMonkey—these functions simply check that data structure to see if the specified field is present. They do not search anywhere else for the property. This means that:

  • The prototype chain of obj is not searched.
  • The object's JSClass.resolve hook is not called, so lazily defined properties are not found. (This is the only API that can directly detect that a lazily resolved property has not yet been resolved.)
  • Shared, permanent, delegated properties are not found. (Such properties are an implementation detail of SpiderMonkey. They are meant to be a transparent optimization; this is the only API that breaks the abstraction.)

For non-native objects, this falls back on a complete search. This calls the JSObjectOps.lookupProperty hook. *foundp is set to true only if lookupProperty reports that the property was found on obj itself and not on some other object (even the corresponding outer object, if any).

If the property is found on obj, this sets *foundp to true and returns true.

If the property is not found on obj, this sets *foundp to false and returns true (to indicate that no error occurred).

If the search fails with an error or exception, this returns false.

See Also

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

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

发布评论

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

词条统计

浏览:23 次

字数:6724

最后编辑:7年前

编辑次数:0 次

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