JS_GetPropertyAttributes

Get the attributes of a specified property.

Syntax

JSBool JS_GetPropertyAttributes(JSContext *cx, JSObject *obj,
    const char *name, uintN *attrsp, JSBool *foundp);

JSBool JS_GetUCPropertyAttributes(JSContext *cx, JSObject *obj,
    const jschar *name, size_t namelen,
    uintN *attrsp, JSBool *foundp);
Name Type Description
cx JSContext * The context in which to look up property attributes. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JSObject * The object that has the property to be queried.
name const char * or const jschar * Name of the property from which to retrieve attributes.
namelen size_t (only in JS_GetUCPropertyAttributes) The length of name in characters; or (size_t) -1 to indicate that name is null-terminated.
attrsp uintN * Out parameter. If the specified property is found on obj, *attrsp receives its attributes.
foundp JSBool * Out parameter. If no error occurs, *foundp receives JS_TRUE if the specified property is found and JS_FALSE if it is not found. If an error occurs, the value left in *foundp is unspecified.

Description

JS_GetPropertyAttributes retrieves the property attributes of the property with the given name on a given object, obj. JS_GetUCPropertyAttributes is the Unicode version of the function.

If an error occurs, the return value is JS_FALSE, and the values left in *attrsp and *foundp are unspecified.

If obj does not have the specified property, or if it inherits it from some other object (on its prototype chain, for example), then *foundp is set to JS_FALSE. The value left in *attrsp is unspecified. The return value is JS_TRUE (to indicate that no error occurred).

If the property exists and belongs to obj, then *foundp is set to JS_TRUE, *attrsp is set to the logical OR of zero or more property attribute flags (described below), and the function returns JS_TRUE.

Property attributes

Any object property can have zero or more property attributes set. Some property attributes are defined in the ECMAScript standard, in ECMA 262-3 §8.6.1. SpiderMonkey additionally defines several non-standard property attributes.

The JSAPI expresses property attributes as a value of type uintN, the bitwise OR of zero or more of the JSPROP flags described below.

Flag Description

JSPROP_ENUMERATE

The property is visible to JavaScript forin and for eachin loops, as well as to JS_Enumerate.

This is the inverse of the ECMA standard DontEnum attribute.

MXR ID Search for JSPROP_ENUMERATE

JSPROP_READONLY

The property's value cannot be set. In JavaScript 1.2 and lower, it is an error to attempt to assign a value to a read-only property. In JavaScript 1.3 and higher, as in ECMAScript, attempts to set a value on a read-only property are ignored.

This is the ECMA standard ReadOnly attribute.

MXR ID Search for JSPROP_READONLY

JSPROP_PERMANENT

The property cannot be deleted. In JavaScript 1.2 and lower, it is an error to attempt to delete a permanent property. In JavaScript 1.3 and higher, as in ECMAScript, such attempts are ignored.

This is the ECMA standard DontDelete attribute.

The JavaScript language does not provide any way for a script to delete a permanent property. C/C++ code can do this by making the property non-permanent, using JS_SetPropertyAttributes, before deleting it.

MXR ID Search for JSPROP_PERMANENT

JSPROP_GETTER

The property's getter is a JavaScript Function, not a C/C++ function. See JS_DefineProperty and JS_GetPropertyAttrsGetterAndSetter for details.

MXR ID Search for JSPROP_GETTER

JSPROP_SETTER

The property's setter is a JavaScript Function, not a C/C++ function. See JS_DefineProperty and JS_GetPropertyAttrsGetterAndSetter for details.

MXR ID Search for JSPROP_SETTER

JSPROP_SHARED

The property is shared. This is usually the right thing for properties that have getters or setters. This has three effects:

  1. The JavaScript engine does not set aside any memory for the property's value. The value is determined entirely by the getter. (The property has no stored value.)
  2. Assignment via the prototype chain is affected. Assigning to obj.x, where obj inherits a non-shared property from its prototype, creates a new own data property on obj; the prototype's .x is not shared with its children. If the inherited property is shared, the setter is called instead.
  3. If the property is also JSPROP_PERMANENT, then certain aspects of inheriting the property via the prototype chain are affected. Do not depend on this. See bug 575997.

MXR ID Search for JSPROP_SHARED

JSPROP_INDEX

The property's id is represented internally as an integer, not a string.

This flag has an additional special meaning when used with JS_DefineProperty, JS_FS, and other APIs that define properties: it means that the name parameter is actually an integer unsafely cast to a pointer type, not a string.

MXR ID Search for JSPROP_INDEX

An application can set property attributes when creating a property. See JS_DefineProperty, JS_FS, and JS_FN. To modify the attributes of an existing property, use JS_SetPropertyAttributes.

MXR ID Search for JS_GetPropertyAttributes
MXR ID Search for JS_GetUCPropertyAttributes

Document Tags and Contributors

Contributors to this page: Tservo, MMondor, Nickolay, Jorend, Dria
Last updated by: Jorend,