JS_DefineElement

Creates a numeric property on a specified object.

Syntax

/* Added in SpiderMonkey 38 (JSAPI 32) */

bool
JS_DefineElement(JSContext *cx, JS::HandleObject obj,
                 uint32_t index, JS::HandleValue value,
                 unsigned attrs,
                 JSNative getter = nullptr, JSNative setter = nullptr);

bool
JS_DefineElement(JSContext *cx, JS::HandleObject obj,
                 uint32_t index, JS::HandleObject value,
                 unsigned attrs,
                 JSNative getter = nullptr, JSNative setter = nullptr);

bool
JS_DefineElement(JSContext *cx, JS::HandleObject obj,
                 uint32_t index, JS::HandleString value,
                 unsigned attrs,
                 JSNative getter = nullptr, JSNative setter = nullptr);

bool
JS_DefineElement(JSContext *cx, JS::HandleObject obj,
                 uint32_t index, int32_t value,
                 unsigned attrs,
                 JSNative getter = nullptr, JSNative setter = nullptr);

bool
JS_DefineElement(JSContext *cx, JS::HandleObject obj,
                 uint32_t index, uint32_t value,
                 unsigned attrs,
                 JSNative getter = nullptr, JSNative setter = nullptr);

bool
JS_DefineElement(JSContext *cx, JS::HandleObject obj,
                 uint32_t index, double value,
                 unsigned attrs,
                 JSNative getter = nullptr, JSNative setter = nullptr);

/* Obsolete since JSAPI 32 */

JS_DefineElement(JSContext *cx, JSObject *obj, uint32_t index, jsval value,
                 JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs);
Name Type Description
cx JSContext * The context in which to create the new property. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JS::HandleObject or JSObject * The object on which to create the new property.
index uint32_t The index of the property to define.
value JS::HandleValue or JS::HandleObject or JS::HandleString or int32_t or uint32_t or double or jsval Initial value to assign to the property.
getter JSNative or JSPropertyOp getProperty method for retrieving the current property value.
setter JSNative or JSStrictPropertyOp setProperty method for specifying a new property value.
flags unsigned Property attributes. Obsolete since JSAPI 32

Description

JS_DefineElement defines a numeric property for a specified object, obj.

index is the index of the element being defined. value is one of JS::Value, JSObject, JSString, int32_t, uint32_t, or double that defines the property's initial value. getter and setter identify the getProperty and setProperty methods for the property, respectively. If you pass NULL values for these entries, JS_DefineElement assigns the default getProperty and setProperty methods to this element.

Obsolete since JSAPI 32 flags contains the property attributes to set for the new property.

On success, JS_DefineElement returns true. Otherwise it returns false.

Obsolete since JSAPI 32.

While you can assign a setProperty method to a property and set flags to JSPROP_READONLY, the setter method will not be called on this property.

See Also