使用 JavaScript 值
¥Working with JavaScript values
Node-API 公开了一组 API 来创建所有类型的 JavaScript 值。其中一些类型记录在 ECMAScript 语言规范 的 第 6 节 下。
¥Node-API exposes a set of APIs to create all types of JavaScript values. Some of these types are documented under Section 6 of the ECMAScript Language Specification.
从根本上说,这些 API 用于执行以下操作之一:
¥Fundamentally, these APIs are used to do one of the following:
-
创建一个新的 JavaScript 对象
¥Create a new JavaScript object
-
从原始 C 类型转换为 Node-API 值
¥Convert from a primitive C type to a Node-API value
-
从 Node-API 值转换为原始 C 类型
¥Convert from Node-API value to a primitive C type
-
获取全局实例包括
undefined
和null
¥Get global instances including
undefined
andnull
Node-API 值由类型 napi_value
表示。任何需要 JavaScript 值的 Node-API 调用都采用 napi_value
。在某些情况下,API 会预先检查 napi_value
的类型。但是,为了获得更好的性能,调用者最好确保所讨论的 napi_value
是 API 期望的 JavaScript 类型。
¥Node-API values are represented by the type napi_value
.
Any Node-API call that requires a JavaScript value takes in a napi_value
.
In some cases, the API does check the type of the napi_value
up-front.
However, for better performance, it's better for the caller to make sure that
the napi_value
in question is of the JavaScript type expected by the API.