napi_new_instance
functionv1js_native_api.hThis method is used to instantiate a new JavaScript value using a given napi_value that represents the constructor for the object. For example, consider the following snippet:
napi_status napi_new_instance(napi_env env,
napi_value cons,
size_t argc,
napi_value* argv,
napi_value* result)
[in] env: The environment that the API is invoked under.[in] cons:napi_valuerepresenting the JavaScript function to be invoked as a constructor.[in] argc: The count of elements in theargvarray.[in] argv: Array of JavaScript values asnapi_valuerepresenting the arguments to the constructor. Ifargcis zero this parameter may be omitted by passing inNULL.[out] result:napi_valuerepresenting the JavaScript object returned, which in this case is the constructed object.
This method is used to instantiate a new JavaScript value using a given
napi_value that represents the constructor for the object. For example,
consider the following snippet:
function MyObject(param) {
this.param = param;
}
const arg = 'hello';
const value = new MyObject(arg);
The following can be approximated in Node-API using the following snippet:
// Get the constructor function MyObject
napi_value global, constructor, arg, value;
napi_status status = napi_get_global(env, &global);
if (status != napi_ok) return;
status = napi_get_named_property(env, global, "MyObject", &constructor);
if (status != napi_ok) return;
// const arg = "hello"
status = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &arg);
if (status != napi_ok) return;
napi_value* argv = &arg;
size_t argc = 1;
// const value = new MyObject(arg)
status = napi_new_instance(env, constructor, argc, argv, &value);
Returns napi_ok if the API succeeded.