JS_GetProperty 编辑

Find a specified property and retrieve its value.

Syntax

bool
JS_GetProperty(JSContext *cx, JS::HandleObject obj, const char *name,
               JS::MutableHandleValue vp);

bool
JS_GetUCProperty(JSContext *cx, JS::HandleObject obj,
                 const char16_t *name, size_t namelen,
                 JS::MutableHandleValue vp);

bool
JS_GetPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
                   JS::MutableHandleValue vp); // Added in SpiderMonkey 1.8.1
NameTypeDescription
cxJSContext *A context. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
objJS::HandleObjectObject to search on for the property.
name or idconst char * or const char16_t * or JS::HandleIdName of the property to look up.
namelensize_t(in JS_GetUCProperty only) The length of name, in characters; or -1 to indicate that name is null-terminated.
vpJS::MutableHandleValueOut parameter. On success, *vp receives the current value of the property, or undefined if no such property is found.

Description

JS_GetProperty examines a specified JS object obj and its prototype chain for a property with the specified name. It behaves like the JavaScript expression obj[name]. JS_GetUCProperty is the Unicode version of the function. JS_GetPropertyById is the same but takes a JS::HandleId for the property name.

In the simplest case, JS_GetProperty stores the value of the property in *vp and returns true. However, several different hooks can affect property gets. The full algorithm is described below.

Details

First, a property lookup is performed. If the lookup proceeds without error, exactly one of the following cases applies:

  • If the property is not found, then *vp is set to undefined. Then the JSClass.getProperty hook of obj's class is called with the arguments (cx, obj, id, vp). For many objects, including objects of standard classes such as Object and Array, this hook does nothing and returns true, so the property get succeeds and undefined is left in *vp.
  • If the property is found on a non-native object, get handler of Proxy (only if the object is a Proxy) or its JSObjectOps.getProperty method is called.
  • Otherwise the property is found on a native object. If the property has a JavaScript getter, it is called. Otherwise *vp is set to the property's stored value, or undefined if the property does not have a stored value, and then the property's getter is called with the arguments (cx, obj, id, vp). For many properties, the getter does nothing and returns true, so the property get succeeds and the property's stored value is left in *vp.

Internally, property retrieval, including all the behavior described above, is implemented by obj's JSObjectOps.getProperty callback.

On success, these functions set *vp to the current value of the property, or undefined if obj has no such property, and return true. On an error or exception, these functions return false, and the value left in *vp is undefined.

See Also

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

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

发布评论

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

词条统计

浏览:95 次

字数:7524

最后编辑:7年前

编辑次数:0 次

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