JS_SetProperty 编辑

Assign a value to a property of an object.

Syntax

bool
JS_SetProperty(JSContext *cx, JS::HandleObject obj, const char *name,
               JS::HandleValue v);

bool
JS_SetUCProperty(JSContext *cx, JS::HandleObject obj,
                 const char16_t *name, size_t namelen,
                 JS::HandleValue v);

bool
JS_SetPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
                   JS::HandleValue v); // Added in SpiderMonkey 1.8.1
NameTypeDescription
cxJSContext *Pointer to a JS context from which to derive runtime information. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
objJS::HandleValueObject to which the property to set belongs.
name or idconst char * or const char16_t *or JS::HandleIdName of the property to set.
namelensize_t(only in JS_SetUCProperty) The length of name in characters; or -1 to indicate that name is null-terminated.
vJS::HandleValueIn/out parameter. *vp is the value to assign to the property. Ordinarily this function leaves v unchanged, but it is possible for a JSClass.addProperty hook or a non-default setter to assign to v.

Description

JS_SetProperty assigns the value v to the property name of the object obj. It behaves like the JavaScript expression obj[name] = v. JS_SetUCProperty is the Unicode version of the function. JS_SetPropertyById is the same but takes a JS::HandleId for the property name.

If obj is a Proxy, the JS_SetProperty [[Set]] internal method defined in ES2015 (rev 29 9.5.9) is performed.

If obj is a native object with custom setGeneric op, the op is called with (cx, obj, id, &v, false)).

Otherwise, [[Set]] internal method defined in ES2015 (rev 29 9.1.9) is performed.

  • If the lookup found no property with the given name, or if it found that obj inherits such a property from a non-native object, then a new own property is added to obj, as described below. The new property is JSPROP_ENUMERATE.
  • Otherwise, if the lookup found a read-only property or obj non-extensible, nothing happens, but this is not an error. In strict mode, a warning is issued.
  • Otherwise, if obj has an own property with the given name, then the existing property's value is set as described below.

Creation of new properties

One of the cases above involves creating a new own property on obj. The name of the new property is given by name or id. The initial stored value of the new property is undefined. Its getter and setter are the JSClass.getProperty and setProperty hooks of obj's class, and its property attributes are exactly JSPROP_ENUMERATE. After the new property is added, the JSClass.addProperty hook is called with the arguments (cx, obj, id, &v). This hook may assign to v. If the hook succeeds and the property has a stored value, then the value left in v by the addProperty hook becomes the stored value of the property.

Setting the value.

Two of the cases above can involve setting the value of a new or existing property of obj. This is done as follows. If the property has a JavaScript setter, it is called; otherwise, if it has a JavaScript getter, then an error is reported; otherwise the property's setter is called, passing the arguments (cx, obj, id, &v). If the setter succeeds and the property has a stored value, then the value left in v becomes the stored value of the property.

On success, JS_SetProperty returns true, and the value in v is left unchanged unless a hook or setter modified it. On error or exception, it returns false, and the value left in v is unspecified.

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

See Also

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

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

发布评论

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

词条统计

浏览:64 次

字数:8132

最后编辑:7年前

编辑次数:0 次

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