The Napi::Object class corresponds to a JavaScript object. It is extended by the following node-addon-api classes that you may use when working with more specific types:
Napi::Value and extends Napi::ArrayNapi::ArrayBufferNapi::Buffer<T>Napi::FunctionNapi::TypedArray.This class provides a number of convenience methods, most of which are used to set or get properties on a JavaScript object. For example, Set() and Get().
#include <napi.h>
using namespace Napi;
Void Init(Env env) {
// Create a new instance
Object obj = Object::New(env);
// Assign values to properties
obj.Set("hello", "world");
obj.Set(uint32_t(42), "The Answer to Life, the Universe, and Everything");
obj.Set("Douglas Adams", true);
// Get properties
Value val1 = obj.Get("hello");
Value val2 = obj.Get(uint32_t(42));
Value val3 = obj.Get("Douglas Adams");
// Test if objects have properties.
bool obj1 = obj.Has("hello"); // true
bool obj2 = obj.Has("world"); // false
}
Napi::Object::Object();
Creates a new empty Object instance.
Napi::Object::Object(napi_env env, napi_value value);
[in] env: The napi_env environment in which to construct the Value object.
[in] value: The C++ primitive from which to instantiate the Value. value may be any of:
Creates a non-empty Napi::Object instance.
Napi::Object Napi::Object::New(napi_env env);
[in] env: The napi_env environment in which to construct the Napi::Value object.Creates a new Napi::Object value.
void Napi::Object::Set (____ key, ____ value);
[in] key: The name for the property being assigned.[in] value: The value being assigned to the property.Add a property with the specified key with the specified value to the object.
The key can be any of the following types:
napi_valueNapi::Valueconst char*const std::string&uint32_tWhile the value must be any of the following types:
napi_valueNapi::Valueconst char*std::string&booldoubleNapi::Value Napi::Object::Get(____ key);
[in] key: The name of the property to return the value for.Returns the Napi::Value associated with the key property. Returns the value undefined if the key does not exist.
The key can be any of the following types:
napi_valueNapi::Valueconst char *const std::string &uint32_tbool Napi::Object::Has (____ key) const;
[in] key: The name of the property to check.Returns a bool that is true if the object has a property named key and false otherwise.
bool Napi::Object::InstanceOf (const Function& constructor) const
[in] constructor: The constructor Napi::Function of the value that is being compared with the object.Returns a bool that is true if the Napi::Object is an instance created by the constructor and false otherwise.
Note: This is equivalent to the JavaScript instanceof operator.
void Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
[in] property: A Napi::PropertyDescriptor.Define a property on the object.
void Napi::Object::DefineProperties (____ properties)
[in] properties: A list of Napi::PropertyDescriptor. Can be one of the following types:
Defines properties on the object.
Napi::PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name);
[in] utf8name: UTF-8 encoded null-terminated property name.Returns a Napi::PropertyLValue as the named property or sets the named property.
Napi::PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name);
[in] utf8name: UTF-8 encoded property name.Returns a Napi::PropertyLValue as the named property or sets the named property.
Napi::PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index);
[in] index: Element index.Returns a Napi::PropertyLValue or sets an indexed property or array element.
Napi::Value Napi::Object::operator[] (const char* utf8name) const;
[in] utf8name: UTF-8 encoded null-terminated property name.Returns the named property as a Napi::Value.
Napi::Value Napi::Object::operator[] (const std::string& utf8name) const;
[in] utf8name: UTF-8 encoded property name.Returns the named property as a Napi::Value.
Napi::Value Napi::Object::operator[] (uint32_t index) const;
[in] index: Element index.Returns an indexed property or array element as a Napi::Value.