Working with JavaScript properties
Source: Node.js documentationNode-API exposes a set of APIs to get and set properties on JavaScript objects.
Properties in JavaScript are represented as a tuple of a key and a value. Fundamentally, all property keys in Node-API can be represented in one of the following forms:
- Named: a simple UTF8-encoded string
- Integer-Indexed: an index value represented by
uint32_t - JavaScript value: these are represented in Node-API by
napi_value. This can be anapi_valuerepresenting astring,number, orsymbol.
Node-API values are represented by the type napi_value.
Any Node-API call that requires a JavaScript value takes in a napi_value.
However, it's the caller's responsibility to make sure that the
napi_value in question is of the JavaScript type expected by the API.
The APIs documented in this section provide a simple interface to
get and set properties on arbitrary JavaScript objects represented by
napi_value.
For instance, consider the following JavaScript code snippet:
const obj = {};
obj.myProp = 123;
The equivalent can be done using Node-API values with the following snippet:
napi_status status = napi_generic_failure;
// const obj = {}
napi_value obj, value;
status = napi_create_object(env, &obj);
if (status != napi_ok) return status;
// Create a napi_value for 123
status = napi_create_int32(env, 123, &value);
if (status != napi_ok) return status;
// obj.myProp = 123
status = napi_set_named_property(env, obj, "myProp", value);
if (status != napi_ok) return status;
Indexed properties can be set in a similar manner. Consider the following JavaScript snippet:
const arr = [];
arr[123] = 'hello';
The equivalent can be done using Node-API values with the following snippet:
napi_status status = napi_generic_failure;
// const arr = [];
napi_value arr, value;
status = napi_create_array(env, &arr);
if (status != napi_ok) return status;
// Create a napi_value for 'hello'
status = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &value);
if (status != napi_ok) return status;
// arr[123] = 'hello';
status = napi_set_element(env, arr, 123, value);
if (status != napi_ok) return status;
Properties can be retrieved using the APIs described in this section. Consider the following JavaScript snippet:
const arr = [];
const value = arr[123];
The following is the approximate equivalent of the Node-API counterpart:
napi_status status = napi_generic_failure;
// const arr = []
napi_value arr, value;
status = napi_create_array(env, &arr);
if (status != napi_ok) return status;
// const value = arr[123]
status = napi_get_element(env, arr, 123, &value);
if (status != napi_ok) return status;
Finally, multiple properties can also be defined on an object for performance reasons. Consider the following JavaScript:
const obj = {};
Object.defineProperties(obj, {
'foo': { value: 123, writable: true, configurable: true, enumerable: true },
'bar': { value: 456, writable: true, configurable: true, enumerable: true },
});
The following is the approximate equivalent of the Node-API counterpart:
napi_status status = napi_status_generic_failure;
// const obj = {};
napi_value obj;
status = napi_create_object(env, &obj);
if (status != napi_ok) return status;
// Create napi_values for 123 and 456
napi_value fooValue, barValue;
status = napi_create_int32(env, 123, &fooValue);
if (status != napi_ok) return status;
status = napi_create_int32(env, 456, &barValue);
if (status != napi_ok) return status;
// Set the properties
napi_property_descriptor descriptors[] = {
{ "foo", NULL, NULL, NULL, NULL, fooValue, napi_writable | napi_configurable, NULL },
{ "bar", NULL, NULL, NULL, NULL, barValue, napi_writable | napi_configurable, NULL }
}
status = napi_define_properties(env,
obj,
sizeof(descriptors) / sizeof(descriptors[0]),
descriptors);
if (status != napi_ok) return status;
Structures
Functions
APIs in this section
Structures
Functions
napi_get_property_namesv1This API returns the names of the enumerable properties of object as an array of strings. The properties of object whose key is a symbol will not be included.napi_get_all_property_namesv6This API returns an array containing the names of the available properties of this object.napi_set_propertyv1This API set a property on the Object passed in.napi_get_propertyv1This API gets the requested property from the Object passed in.napi_has_propertyv1This API checks if the Object passed in has the named property.napi_delete_propertyv1This API attempts to delete the key own property from object.napi_has_own_propertyv1This API checks if the Object passed in has the named own property. key must be a string or a symbol, or an error will be thrown. Node-API will not perform any conversion between data types.napi_set_named_propertyv1This method is equivalent to calling napi_set_property with a napi_value created from the string passed in as utf8Name.napi_get_named_propertyv1This method is equivalent to calling napi_get_property with a napi_value created from the string passed in as utf8Name.napi_has_named_propertyv1This method is equivalent to calling napi_has_property with a napi_value created from the string passed in as utf8Name.napi_set_elementv1This API sets an element on the Object passed in.napi_get_elementv1This API gets the element at the requested index.napi_has_elementv1This API returns if the Object passed in has an element at the requested index.napi_delete_elementv1This API attempts to delete the specified index from object.napi_define_propertiesv1This method allows the efficient definition of multiple properties on a given object. The properties are defined using property descriptors (see napi_property_descriptor).napi_object_freezev8This method freezes a given object. This prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing…napi_object_sealv8This method seals a given object. This prevents new properties from being added to it, as well as marking all existing properties as non-configurable. This is described in Section 19.1.2.20 of the ECMA-262 specification.node_api_set_prototypeexperimentalThis API sets the prototype of the Object passed in.