napi_create_object_with_properties
¥Stability: 1 - Experimental
napi_status napi_create_object_with_properties(napi_env env,
napi_value prototype_or_null,
const napi_value* property_names,
const napi_value* property_values,
size_t property_count,
napi_value* result) -
[in] env:调用 API 的环境。¥
[in] env: The environment that the API is invoked under. -
[in] prototype_or_null:新对象的原型对象。可以是表示用作原型的 JavaScript 对象的napi_value,表示 JavaScript 的napi_value(null),或将转换为null的nullptr。¥
[in] prototype_or_null: The prototype object for the new object. Can be anapi_valuerepresenting a JavaScript object to use as the prototype, anapi_valuerepresenting JavaScriptnull, or anullptrthat will be converted tonull. -
[in] property_names:表示属性名称的napi_value数组。¥
[in] property_names: Array ofnapi_valuerepresenting the property names. -
[in] property_values:表示属性值的napi_value数组。¥
[in] property_values: Array ofnapi_valuerepresenting the property values. -
[in] property_count:数组中的属性数量。¥
[in] property_count: Number of properties in the arrays. -
[out] result:代表 JavaScriptObject的napi_value。¥
[out] result: Anapi_valuerepresenting a JavaScriptObject.
如果 API 成功,则返回 napi_ok。
¥Returns napi_ok if the API succeeded.
此 API 使用指定的原型和属性创建一个 JavaScript Object。此方法比先调用 napi_create_object 再多次调用 napi_set_property 更高效,因为它能够原子地创建包含所有属性的对象,从而避免潜在的 V8 映射转换。
¥This API creates a JavaScript Object with the specified prototype and
properties. This is more efficient than calling napi_create_object followed
by multiple napi_set_property calls, as it can create the object with all
properties atomically, avoiding potential V8 map transitions.
数组 property_names 和 property_values 的长度必须与 property_count 指定的长度相同。属性会按照它们在数组中出现的顺序添加到对象中。
¥The arrays property_names and property_values must have the same length
specified by property_count. The properties are added to the object in the
order they appear in the arrays.