napi_define_class
napi_status napi_define_class(napi_env env,
const char* utf8name,
size_t length,
napi_callback constructor,
void* data,
size_t property_count,
const napi_property_descriptor* properties,
napi_value* result); -
[in] env:调用 API 的环境。¥
[in] env: The environment that the API is invoked under. -
[in] utf8name:JavaScript 构造函数的名称。为清楚起见,建议在封装 C++ 类时使用 C++ 类名。¥
[in] utf8name: Name of the JavaScript constructor function. For clarity, it is recommended to use the C++ class name when wrapping a C++ class. -
[in] length:utf8name的长度(以字节为单位)或NAPI_AUTO_LENGTH(如果以 null 结尾)。¥
[in] length: The length of theutf8namein bytes, orNAPI_AUTO_LENGTHif it is null-terminated. -
[in] constructor:处理类的构造实例的回调函数。封装 C++ 类时,此方法必须是具有napi_callback签名的静态成员。不能使用 C++ 类构造函数。napi_callback提供了更多详细信息。¥
[in] constructor: Callback function that handles constructing instances of the class. When wrapping a C++ class, this method must be a static member with thenapi_callbacksignature. A C++ class constructor cannot be used.napi_callbackprovides more details. -
[in] data:作为回调信息的data属性传递给构造函数回调的可选数据。¥
[in] data: Optional data to be passed to the constructor callback as thedataproperty of the callback info. -
[in] property_count:properties数组参数中的项数。¥
[in] property_count: Number of items in thepropertiesarray argument. -
[in] properties:描述类静态和实例数据属性、访问器和方法的属性描述符数组 请参见napi_property_descriptor。¥
[in] properties: Array of property descriptors describing static and instance data properties, accessors, and methods on the class Seenapi_property_descriptor. -
[out] result:表示类的构造函数的napi_value。¥
[out] result: Anapi_valuerepresenting the constructor function for the class.
如果 API 成功,则返回 napi_ok。
¥Returns napi_ok if the API succeeded.
定义一个 JavaScript 类,包括:
¥Defines a JavaScript class, including:
-
具有类名的 JavaScript 构造函数。当封装相应的 C++ 类时,通过
constructor传递的回调可用于实例化一个新的 C++ 类实例,然后将其放置在使用napi_wrap构造的 JavaScript 对象实例中。¥A JavaScript constructor function that has the class name. When wrapping a corresponding C++ class, the callback passed via
constructorcan be used to instantiate a new C++ class instance, which can then be placed inside the JavaScript object instance being constructed usingnapi_wrap. -
构造函数上的属性,其实现可以调用 C++ 类的相应静态数据属性、访问器和方法(由具有
napi_static属性的属性描述符定义)。¥Properties on the constructor function whose implementation can call corresponding static data properties, accessors, and methods of the C++ class (defined by property descriptors with the
napi_staticattribute). -
构造函数的
prototype对象的属性。封装 C++ 类时,在检索放置在 JavaScript 对象实例中的 C++ 类实例后,可以从属性描述符中给定的静态函数调用 C++ 类的非静态数据属性、访问器和方法,而无需使用napi_static属性napi_unwrap。¥Properties on the constructor function's
prototypeobject. When wrapping a C++ class, non-static data properties, accessors, and methods of the C++ class can be called from the static functions given in the property descriptors without thenapi_staticattribute after retrieving the C++ class instance placed inside the JavaScript object instance by usingnapi_unwrap.
封装 C++ 类时,通过 constructor 传递的 C++ 构造函数回调应该是调用实际类构造函数的类的静态方法,然后将新的 C++ 实例封装在 JavaScript 对象中,并返回封装器对象。详见 napi_wrap。
¥When wrapping a C++ class, the C++ constructor callback passed via constructor
should be a static method on the class that calls the actual class constructor,
then wraps the new C++ instance in a JavaScript object, and returns the wrapper
object. See napi_wrap for details.
从 napi_define_class 返回的 JavaScript 构造函数通常被保存并在以后用于从原生代码构造类的新实例,和/或检查提供的值是否是类的实例。在这种情况下,为了防止函数值被垃圾收集,可以使用 napi_create_reference 创建对其的强持久引用,确保引用计数保持 >= 1。
¥The JavaScript constructor function returned from napi_define_class is
often saved and used later to construct new instances of the class from native
code, and/or to check whether provided values are instances of the class. In
that case, to prevent the function value from being garbage-collected, a
strong persistent reference to it can be created using
napi_create_reference, ensuring that the reference count is kept >= 1.
通过 data 参数或 napi_property_descriptor 数组项的 data 字段传递给此 API 的任何非 NULL 数据都可以与生成的 JavaScript 构造函数(在 result 参数中返回)相关联,并在类为垃圾时释放 - 通过将 JavaScript 函数和数据传递给 napi_add_finalizer 来收集。
¥Any non-NULL data which is passed to this API via the data parameter or via
the data field of the napi_property_descriptor array items can be associated
with the resulting JavaScript constructor (which is returned in the result
parameter) and freed whenever the class is garbage-collected by passing both
the JavaScript function and the data to napi_add_finalizer.