Working with JavaScript functions
Source: Node.js documentationNode-API provides a set of APIs that allow JavaScript code to
call back into native code. Node-APIs that support calling back
into native code take in a callback functions represented by
the napi_callback type. When the JavaScript VM calls back to
native code, the napi_callback function provided is invoked. The APIs
documented in this section allow the callback function to do the
following:
- Get information about the context in which the callback was invoked.
- Get the arguments passed into the callback.
- Return a
napi_valueback from the callback.
Additionally, Node-API provides a set of functions which allow calling JavaScript functions from native code. One can either call a function like a regular JavaScript function call, or as a constructor function.
Any non-NULL data which is passed to this API via the data field of the
napi_property_descriptor items can be associated with object and freed
whenever object is garbage-collected by passing both object and the data to
napi_add_finalizer.
APIs in this section
napi_call_functionv1This method allows a JavaScript function object to be called from a native add-on. This is the primary mechanism of calling back _from_ the add-on's native code _into_ JavaScript.napi_create_functionv1This API allows an add-on author to create a function object in native code. This is the primary mechanism to allow calling _into_ the add-on's native code _from_ JavaScript.napi_get_cb_infov1This method is used within a callback function to retrieve details about the call like the arguments and the this pointer from a given callback info.napi_get_new_targetv1This API returns the new.target of the constructor call. If the current callback is not a constructor call, the result is NULL.napi_new_instancev1This 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: