N-API exposes a set of APIs to get and set properties on JavaScript objects. Some of these types are documented under [Section 7][] of the [ECMAScript Language Specification][].

    Properties in JavaScript are represented as a tuple of a key and a value. Fundamentally, all property keys in N-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 N-API by napi_value. This can be a napi_value representing a String, Number, or Symbol.

    N-API values are represented by the type napi_value. Any N-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:

    1. const obj = {};
    2. obj.myProp = 123;

    The equivalent can be done using N-API values with the following snippet:

    1. napi_status status = napi_generic_failure;
    2. // const obj = {}
    3. napi_value obj, value;
    4. status = napi_create_object(env, &obj);
    5. if (status != napi_ok) return status;
    6. // Create a napi_value for 123
    7. status = napi_create_int32(env, 123, &value);
    8. if (status != napi_ok) return status;
    9. // obj.myProp = 123
    10. status = napi_set_named_property(env, obj, "myProp", value);
    11. if (status != napi_ok) return status;

    Indexed properties can be set in a similar manner. Consider the following JavaScript snippet:

    1. const arr = [];
    2. arr[123] = 'hello';

    The equivalent can be done using N-API values with the following snippet:

    1. napi_status status = napi_generic_failure;
    2. // const arr = [];
    3. napi_value arr, value;
    4. status = napi_create_array(env, &arr);
    5. if (status != napi_ok) return status;
    6. // Create a napi_value for 'hello'
    7. status = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &value);
    8. if (status != napi_ok) return status;
    9. // arr[123] = 'hello';
    10. status = napi_set_element(env, arr, 123, value);
    11. if (status != napi_ok) return status;

    Properties can be retrieved using the APIs described in this section. Consider the following JavaScript snippet:

    1. const arr = [];
    2. const value = arr[123];

    The following is the approximate equivalent of the N-API counterpart:

    1. napi_status status = napi_generic_failure;
    2. // const arr = []
    3. napi_value arr, value;
    4. status = napi_create_array(env, &arr);
    5. if (status != napi_ok) return status;
    6. // const value = arr[123]
    7. status = napi_get_element(env, arr, 123, &value);
    8. if (status != napi_ok) return status;

    Finally, multiple properties can also be defined on an object for performance reasons. Consider the following JavaScript:

    1. const obj = {};
    2. Object.defineProperties(obj, {
    3. 'foo': { value: 123, writable: true, configurable: true, enumerable: true },
    4. 'bar': { value: 456, writable: true, configurable: true, enumerable: true }
    5. });

    The following is the approximate equivalent of the N-API counterpart:

    1. napi_status status = napi_status_generic_failure;
    2. // const obj = {};
    3. napi_value obj;
    4. status = napi_create_object(env, &obj);
    5. if (status != napi_ok) return status;
    6. // Create napi_values for 123 and 456
    7. napi_value fooValue, barValue;
    8. status = napi_create_int32(env, 123, &fooValue);
    9. if (status != napi_ok) return status;
    10. status = napi_create_int32(env, 456, &barValue);
    11. if (status != napi_ok) return status;
    12. // Set the properties
    13. napi_property_descriptor descriptors[] = {
    14. { "foo", NULL, NULL, NULL, NULL, fooValue, napi_writable | napi_configurable, NULL },
    15. { "bar", NULL, NULL, NULL, NULL, barValue, napi_writable | napi_configurable, NULL }
    16. }
    17. status = napi_define_properties(env,
    18. obj,
    19. sizeof(descriptors) / sizeof(descriptors[0]),
    20. descriptors);
    21. if (status != napi_ok) return status;