news 2026/4/16 12:56:50

Property Descriptor

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Property Descriptor

属性描述符(Property Descriptor)

Napi::Object可通过其DefinePropertyDefineProperties方法为自身分配属性,这两个方法均接收PropertyDescriptor(属性描述符)作为参数。Napi::PropertyDescriptor可包含值或函数,这些值 / 函数会被分配给目标Napi::Object。请注意:单个Napi::PropertyDescriptor类实例只能包含一个值,或最多两个函数;属性描述符仅能通过该类的AccessorFunctionValue方法创建 —— 这些方法都会返回一个新的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::FunctionNapi::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/4/16 0:41:28

Evolve数据库迁移工具:5分钟快速上手指南

Evolve数据库迁移工具&#xff1a;5分钟快速上手指南 【免费下载链接】Evolve lecaillon/Evolve: 是一个基于遗传算法的简单演化计算框架&#xff0c;可以用于解决优化问题。适合用于学习和研究演化计算和优化问题&#xff0c;以及进行相关的算法实现和实验。 项目地址: http…

作者头像 李华
网站建设 2026/4/16 10:16:31

如何快速掌握SwiftUI富文本编辑:RichTextKit终极教程

如何快速掌握SwiftUI富文本编辑&#xff1a;RichTextKit终极教程 【免费下载链接】RichTextKit RichTextKit is a Swift-based library for working with rich text in UIKit, AppKit and SwiftUI. 项目地址: https://gitcode.com/gh_mirrors/ri/RichTextKit 还在为Swif…

作者头像 李华
网站建设 2026/4/16 10:14:16

多模态智能体的记忆革命:从瞬时交互到持续认知的范式跃迁

多模态智能体的记忆革命&#xff1a;从瞬时交互到持续认知的范式跃迁 【免费下载链接】M3-Agent-Memorization 项目地址: https://ai.gitcode.com/hf_mirrors/ByteDance-Seed/M3-Agent-Memorization 当AI系统能够像人类一样记住过往经历&#xff0c;从每次交互中积累经…

作者头像 李华
网站建设 2026/4/16 10:16:15

YOLO实时检测在自动驾驶中的应用:背后离不开强大GPU支撑

YOLO实时检测在自动驾驶中的应用&#xff1a;背后离不开强大GPU支撑引言 技术背景 随着人工智能技术的飞速发展&#xff0c;计算机视觉已成为推动智能系统演进的核心驱动力之一。在众多视觉任务中&#xff0c;目标检测作为感知环境的关键环节&#xff0c;在自动驾驶、工业质检、…

作者头像 李华