napi_create_object_with_properties


稳定性: 1 - 实验性

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] prototype_or_null:新对象的原型对象。可以是表示 JavaScript 对象的 napi_value 用作原型,也可以是表示 JavaScript nullnapi_value,或者是会被转换为 nullnullptr
  • [in] property_names:表示属性名称的 napi_value 数组。
  • [in] property_values:表示属性值的 napi_value 数组。
  • [in] property_count:数组中的属性数量。
  • [out] result:一个表示 JavaScript Objectnapi_value

如果 API 成功,则返回 napi_ok

【Returns napi_ok if the API succeeded.】

此 API 会创建一个具有指定原型和属性的 JavaScript Object。这比先调用 napi_create_object 然后多次调用 napi_set_property 更高效,因为它可以原子地创建具有所有属性的对象,避免了潜在的 V8 map 转换。

【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_namesproperty_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.】