news 2026/10/27 17:48:20

Property Descriptor

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Property Descriptor

属性描述符(Property Descriptor)

Napi::Object可通过其DefineProperty和DefineProperties方法为自身分配属性,这两个方法均接收PropertyDescriptor(属性描述符)作为参数。Napi::PropertyDescriptor可包含值或函数,这些值 / 函数会被分配给目标Napi::Object。请注意:单个Napi::PropertyDescriptor类实例只能包含一个值,或最多两个函数;属性描述符仅能通过该类的Accessor、Function或Value方法创建 —— 这些方法都会返回一个新的Napi::PropertyDescriptor静态实例。

示例

#include <napi.h> using namespace Napi; Value TestGetter(const CallbackInfo& info) { return Boolean::New(info.Env(), testValue); } void TestSetter(const CallbackInfo& info) { testValue = info[0].As<Boolean>(); } Value TestFunction(const CallbackInfo& info) { return Boolean::New(info.Env(), true); } void Init(Env env) { // 创建一个对象 Object obj = Object::New(env); // 只读访问器 PropertyDescriptor pd1 = PropertyDescriptor::Accessor<TestGetter>("pd1"); // 读写访问器 PropertyDescriptor pd2 = PropertyDescriptor::Accessor<TestGetter, TestSetter>("pd2"); // 函数属性 PropertyDescriptor pd3 = PropertyDescriptor::Function(env, "function", TestFunction); // 值属性 Boolean true_bool = Boolean::New(env, true); PropertyDescriptor pd4 = PropertyDescriptor::Value("boolean value", Napi::Boolean::New(env, true), napi_writable); // 为对象批量定义属性 obj.DefineProperties({pd1, pd2, pd3, pd4}); }

类型(Types)

PropertyDescriptor::GetterCallback

using GetterCallback = Napi::Value (*)(const Napi::CallbackInfo& info);

这是作为模板参数传递给PropertyDescriptor::Accessor的获取器函数签名。

PropertyDescriptor::SetterCallback

using SetterCallback = void (*)(const Napi::CallbackInfo& info);

这是作为模板参数传递给PropertyDescriptor::Accessor的设置器函数签名。

方法(Methods)

构造函数(Constructor)

Napi::PropertyDescriptor::PropertyDescriptor (napi_property_descriptor desc);
  • [in] desc:用于创建新属性描述符的napi_property_descriptor实例。

Accessor(访问器)

template <Napi::PropertyDescriptor::GetterCallback Getter> static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, napi_property_attributes attributes = napi_default, void* data = nullptr);
  • [模板参数] Getter:获取器函数。
  • [in] attributes:获取器函数的特性标志(可选)。
  • [in] data:任意类型的数据指针,默认值为空指针。

返回包含只读属性的属性描述符。

属性名称支持以下类型:

  • const char*
  • const std::string &
  • napi_value value
  • Napi::Name
template < Napi::PropertyDescriptor::GetterCallback Getter, Napi::PropertyDescriptor::SetterCallback Setter> static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, napi_property_attributes attributes = napi_default, void* data = nullptr);
  • [模板参数] Getter:获取器函数。
  • [模板参数] Setter:设置器函数。
  • [in] attributes:获取器函数的特性标志(可选)。
  • [in] data:任意类型的数据指针,默认值为空指针。

返回包含读写属性的属性描述符。

属性名称支持以下类型:

  • const char*
  • const std::string &
  • napi_value value
  • Napi::Name
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, Getter getter, napi_property_attributes attributes = napi_default, void *data = nullptr);
  • [in] name:获取器函数的名称。
  • [in] getter:获取器函数。
  • [in] attributes:获取器函数的特性标志(可选)。
  • [in] data:任意类型的数据指针,默认值为空指针。

返回包含单个函数的属性描述符。

属性名称支持以下类型:

  • const char*
  • const std::string &
  • napi_value value
  • Napi::Name

⚠️ 注意:该签名已废弃,使用会导致内存泄漏。

static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor ( Napi::Env env, Napi::Object object, ___ name, Getter getter, napi_property_attributes attributes = napi_default, void *data = nullptr);
  • [in] env:创建该访问器的运行环境。
  • [in] object:要定义访问器的目标对象。
  • [in] name:获取器函数的名称。
  • [in] getter:获取器函数。
  • [in] attributes:获取器函数的特性标志(可选)。
  • [in] data:任意类型的数据指针,默认值为空指针。

返回包含 Getter 访问器的Napi::PropertyDescriptor。

属性名称支持以下类型:

  • const char*
  • const std::string &
  • Napi::Name
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, Getter getter, Setter setter, napi_property_attributes attributes = napi_default, void *data = nullptr);
  • [in] name:获取器和设置器函数的名称。
  • [in] getter:获取器函数。
  • [in] setter:设置器函数。
  • [in] attributes:获取器函数的特性标志(可选)。
  • [in] data:任意类型的数据指针,默认值为空指针。

返回包含 Getter 和 Setter 函数的属性描述符。

属性名称支持以下类型:

  • const char*
  • const std::string &
  • napi_value value
  • Napi::Name

⚠️ 注意:该签名已废弃,使用会导致内存泄漏。

static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor ( Napi::Env env, Napi::Object object, ___ name, Getter getter, Setter setter, napi_property_attributes attributes = napi_default, void *data = nullptr);
  • [in] env:创建该访问器的运行环境。
  • [in] object:要定义访问器的目标对象。
  • [in] name:获取器和设置器函数的名称。
  • [in] getter:获取器函数。
  • [in] setter:设置器函数。
  • [in] attributes:获取器函数的特性标志(可选)。
  • [in] data:任意类型的数据指针,默认值为空指针。

返回包含 Getter 和 Setter 函数的Napi::PropertyDescriptor。

属性名称支持以下类型:

  • const char*
  • const std::string &
  • Napi::Name

Function(函数)

static Napi::PropertyDescriptor Napi::PropertyDescriptor::Function (___ name, Callable cb, napi_property_attributes attributes = napi_default, void *data = nullptr);
  • [in] name:可调用函数的名称。
  • [in] cb:目标函数。
  • [in] attributes:函数的特性标志(可选)。
  • [in] data:任意类型的数据指针,默认值为空指针。

返回包含可调用Napi::Function的属性描述符。

属性名称支持以下类型:

  • const char*
  • const std::string &
  • napi_value value
  • Napi::Name

⚠️ 注意:该签名已废弃,使用会导致内存泄漏。

static Napi::PropertyDescriptor Napi::PropertyDescriptor::Function ( Napi::Env env, ___ name, Callable cb, napi_property_attributes attributes = napi_default, void *data = nullptr);
  • [in] env:创建该函数属性的运行环境。
  • [in] name:可调用函数的名称。
  • [in] cb:目标函数。
  • [in] attributes:函数的特性标志(可选)。
  • [in] data:任意类型的数据指针,默认值为空指针。

返回包含可调用Napi::Function的Napi::PropertyDescriptor。

属性名称支持以下类型:

  • const char*
  • const std::string &
  • Napi::Name

Value(值)

static Napi::PropertyDescriptor Napi::PropertyDescriptor::Value (___ name, napi_value value, napi_property_attributes attributes = napi_default);

属性名称支持以下类型:

  • const char*
  • const std::string &
  • napi_value value
  • Napi::Name

相关信息(Related Information)

napi_property_attributes

napi_property_attributes是一组标志,用于告知 JavaScript 该属性应具备的访问权限。可选标志如下:

  • napi_default(默认权限)
  • napi_writable(可写)
  • napi_enumerable(可枚举)
  • napi_configurable(可配置)

如需了解这些标志及napi_property_attributes的更多细节,请参考此处。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/27 9:40:30

Sigma移动安全检测:从零构建Android与iOS威胁防御体系 [特殊字符]️

在数字化浪潮席卷全球的今天&#xff0c;移动设备已成为企业数据泄露的主要风险点。Sigma框架作为开源威胁检测的标准化语言&#xff0c;正在彻底改变我们对移动安全监测的认知方式。无论你是安全分析师还是移动应用开发者&#xff0c;掌握Sigma在Android日志检测和iOS威胁分析…

作者头像 李华
网站建设 2026/10/27 10:01:39

pytest高效测试实战:5大核心技巧提升Python代码质量

pytest高效测试实战&#xff1a;5大核心技巧提升Python代码质量 【免费下载链接】pytest The pytest framework makes it easy to write small tests, yet scales to support complex functional testing 项目地址: https://gitcode.com/gh_mirrors/py/pytest 想要构建可…

作者头像 李华
网站建设 2026/10/27 10:01:40

通达信Day文件转换工具:金融数据处理的终极解决方案

通达信Day文件转换工具&#xff1a;金融数据处理的终极解决方案 【免费下载链接】通达信day格式文件转换工具含港股和基金等 本资源文件提供了一个将通达信day格式文件转换为csv文件的工具。该工具支持上证、深证、港股等市场的数据转换&#xff0c;并对股票、基金、港股等不同…

作者头像 李华
网站建设 2026/10/27 10:01:40

Soybean Admin 架构深度解析:从Vue到React的跨框架设计思维

Soybean Admin 架构深度解析&#xff1a;从Vue到React的跨框架设计思维 【免费下载链接】soybean-admin A clean, elegant, beautiful and powerful admin template, based on Vue3, Vite6, TypeScript, Pinia, NaiveUI and UnoCSS. 一个清新优雅、高颜值且功能强大的后台管理模…

作者头像 李华
网站建设 2026/10/27 10:01:40

CosyVoice语音合成终极指南:从零开始构建智能语音系统

CosyVoice语音合成终极指南&#xff1a;从零开始构建智能语音系统 【免费下载链接】CosyVoice Multi-lingual large voice generation model, providing inference, training and deployment full-stack ability. 项目地址: https://gitcode.com/gh_mirrors/cos/CosyVoice …

作者头像 李华