Node.js N-API napi_define_class

🌙
手机阅读
本文目录结构

Node.js N-API napi_define_class

新增于: v8.0.0 N-API version: 1 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: The environment that the API is invoked under. [in] utf8name: Name of the JavaScript constructor function; this is not required to be the same as the C++ class name, though it is recommended for clarity. [in] length: The length of the utf8name in bytes, or NAPI_AUTO_LENGTH if it is null-terminated. [in] constructor: Callback function that handles constructing instances of the class. (This should be a static method on the class, not an actual C++ constructor function.) [in] data: Optional data to be passed to the constructor callback as the data property of the callback info. [in] property_count: Number of items in the properties array argument. [in] properties: Array of property descriptors describing static and instance data properties, accessors, and methods on the class See napi_property_descriptor. [out] result: A napi_value representing the constructor function for the class. Returns napi_ok if the API succeeded.

Defines a JavaScript class that corresponds to a C++ class, including:

A JavaScript constructor function that has the class name and invokes the provided C++ constructor callback. Properties on the constructor function corresponding to static data properties, accessors, and methods of the C++ class (defined by property descriptors with the napi_static attribute). Properties on the constructor function’s prototype object corresponding to non-static data properties, accessors, and methods of the C++ class (defined by property descriptors without the napi_static attribute). The C++ constructor callback 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.

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 check whether provided values are instances of the class. In that case, to prevent the function value from being garbage-collected, create a persistent reference to it using napi_create_reference and ensure the reference count is kept >= 1.

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.


更多内容请参考:Node.js N-API,或者通过 点击对应菜单 进行查看;



AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

目前重心已经放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>