This article is in need of a technical review.
Assign a value to a property of an object.
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
||Pointer to a JS context from which to derive runtime information. Requires request. In a
||Object to which the property to set belongs.|
||Name of the property to set.|
||In/out parameter. *
JS_SetProperty assigns the value
v to the property
name of the object
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.
obj is a
JS_SetProperty [[Set]] internal method defined in ES6 (rev 29 9.5.9) is performed.
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 ES6 (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
- Otherwise, if the lookup found a read-only property or
objnon-extensible, nothing happens, but this is not an error. In strict mode, a warning is issued.
- Otherwise, if
objhas 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
id. The initial stored value of the new property is
undefined. Its getter and setter are the
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
(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.
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