|
1 | 1 | # Object
|
2 | 2 |
|
3 |
| -You are reading a draft of the next documentation and it's in continuous update so |
4 |
| -if you don't find what you need please refer to: |
5 |
| -[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) |
| 3 | +The 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: |
| 4 | + |
| 5 | +- [Value](value.md) and extends [Array](array.md) |
| 6 | +- [ArrayBuffer](array_buffer.md) |
| 7 | +- [Buffer<T>](buffer.md) |
| 8 | +- [Function](function.md) |
| 9 | +- [TypedArray](typed_array.md). |
| 10 | + |
| 11 | +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(). |
| 12 | + |
| 13 | +## Example |
| 14 | +```cpp |
| 15 | +#include <napi.h> |
| 16 | + |
| 17 | +using namespace Napi; |
| 18 | + |
| 19 | +Void Init(Env env) { |
| 20 | + |
| 21 | + // Create a new instance |
| 22 | + Object obj = Object::New(env); |
| 23 | + |
| 24 | + // Assign values to properties |
| 25 | + obj.Set("hello", "world"); |
| 26 | + obj.Set(42, "The Answer to Life, the Universe, and Everything"); |
| 27 | + obj.Set("Douglas Adams", true); |
| 28 | + |
| 29 | + // Get properties |
| 30 | + Value val1 = obj.Get("hello"); |
| 31 | + Value val2 = obj.Get(42); |
| 32 | + Value val3 = obj.Get("Douglas Adams"); |
| 33 | + |
| 34 | + // Test if objects have properties. |
| 35 | + bool obj1 = obj.Has("hello"); // true |
| 36 | + bool obj2 = obj.Has("world"); // false |
| 37 | + |
| 38 | +} |
| 39 | +``` |
| 40 | +
|
| 41 | +## Methods |
| 42 | +
|
| 43 | +### Empty Constructor |
| 44 | +
|
| 45 | +```cpp |
| 46 | +Napi::Object::Object(); |
| 47 | +``` |
| 48 | +Creates a new empty Object instance. |
| 49 | + |
| 50 | +### Constructor |
| 51 | + |
| 52 | +```cpp |
| 53 | +Napi::Object::Object(napi_env env, napi_value value); |
| 54 | +``` |
| 55 | +- `[in] env`: The `napi_env` environment in which to construct the Value object. |
| 56 | +
|
| 57 | +- `[in] value`: The C++ primitive from which to instantiate the Value. `value` may be any of: |
| 58 | + - bool |
| 59 | + - Any integer type |
| 60 | + - Any floating point type |
| 61 | + - const char* (encoded using UTF-8, null-terminated) |
| 62 | + - const char16_t* (encoded using UTF-16-LE, null-terminated) |
| 63 | + - std::string (encoded using UTF-8) |
| 64 | + - std::u16string |
| 65 | + - napi::Value |
| 66 | + - napi_value |
| 67 | +
|
| 68 | +Creates a non-empty Object instance. |
| 69 | +
|
| 70 | +### New() |
| 71 | +
|
| 72 | +```cpp |
| 73 | +Object Napi::Object::New(napi_env env); |
| 74 | +``` |
| 75 | +- `[in] env`: The `napi_env` environment in which to construct the Value object. |
| 76 | + |
| 77 | +Creates a new Object value. |
| 78 | + |
| 79 | +### Set() |
| 80 | + |
| 81 | +```cpp |
| 82 | +void Napi::Object::Set (____ key, ____ value); |
| 83 | +``` |
| 84 | +- `[in] key`: The name for the property being assigned. |
| 85 | +- `[in] value`: The value being assigned to the property. |
| 86 | +
|
| 87 | +Add a property with the specified key with the specified value to the object. |
| 88 | +
|
| 89 | +The key can be any of the following types: |
| 90 | +- `napi_value` |
| 91 | +- [Value](value.md) |
| 92 | +- `const char*` |
| 93 | +- `const std::string&` |
| 94 | +- `uint32_t` |
| 95 | +
|
| 96 | +While the value must be any of the following types: |
| 97 | +- `napi_value` |
| 98 | +- [Value](value.md) |
| 99 | +- `const char*` |
| 100 | +- `std::string&` |
| 101 | +- `bool` |
| 102 | +- `double` |
| 103 | +
|
| 104 | +### Get() |
| 105 | +
|
| 106 | +```cpp |
| 107 | +Value Napi::Object::Get(____ key); |
| 108 | +``` |
| 109 | +- `[in] key`: The name of the property to return the value for. |
| 110 | + |
| 111 | +Returns the [Value](value.md) associated with the key property. Returns NULL if no such key exists. |
| 112 | + |
| 113 | +The `key` can be any of the following types: |
| 114 | +- `napi_value` |
| 115 | +- [Value](value.md) |
| 116 | +- `const char *` |
| 117 | +- `const std::string &` |
| 118 | +- `uint32_t` |
| 119 | + |
| 120 | +### Has() |
| 121 | + |
| 122 | +```cpp |
| 123 | +bool Napi::Object::Has (____ key) const; |
| 124 | +``` |
| 125 | +- `[in] key`: The name of the property to check. |
| 126 | +
|
| 127 | +Returns a `bool` that is *true* if the object has a property named `key` and *false* otherwise. |
| 128 | +
|
| 129 | +### InstanceOf() |
| 130 | +
|
| 131 | +```cpp |
| 132 | +bool Napi::Object::InstanceOf (const Function& constructor) const |
| 133 | +``` |
| 134 | +- `[in] constructor`: The constructor [Function](function.md) of the value that is being compared with the object. |
| 135 | + |
| 136 | +Returns a `bool` that is true if the Object is an instance created by the `constructor` and false otherwise. |
| 137 | + |
| 138 | +Note: This is equivalent to the JavaScript instanceof operator. |
| 139 | + |
| 140 | +### DefineProperty() |
| 141 | + |
| 142 | +```cpp |
| 143 | +void Napi::Object::DefineProperty (const PropertyDescriptor& property); |
| 144 | +``` |
| 145 | +- `[in] property`: A [PropertyDescriptor](propertydescriptor.md). |
| 146 | +
|
| 147 | +Define a property on the object. |
| 148 | +
|
| 149 | +### DefineProperties() |
| 150 | +
|
| 151 | +```cpp |
| 152 | +void Napi::Object::DefineProperties (____ properties) |
| 153 | +``` |
| 154 | +- `[in] properties`: A list of [PropertyDescriptor](propertydescriptor.md). Can be one of the following types: |
| 155 | + - const std::initializer_list<PropertyDescriptor>& |
| 156 | + - const std::vector<PropertyDescriptor>& |
| 157 | + |
| 158 | +Defines properties on the object. |
| 159 | + |
| 160 | +### Operator[]() |
| 161 | + |
| 162 | +```cpp |
| 163 | +PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name); |
| 164 | +``` |
| 165 | +- `[in] utf8name`: UTF-8 encoded null-terminated property name. |
| 166 | + |
| 167 | +Returns a [PropertyLValue](propertylvalue.md) as the named property or sets the named property. |
| 168 | + |
| 169 | +```cpp |
| 170 | +PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name); |
| 171 | +``` |
| 172 | +- `[in] utf8name`: UTF-8 encoded property name. |
| 173 | + |
| 174 | +Returns a [PropertyLValue](propertylvalue.md) as the named property or sets the named property. |
| 175 | + |
| 176 | +```cpp |
| 177 | +PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index); |
| 178 | +``` |
| 179 | +- `[in] index`: Element index. |
| 180 | + |
| 181 | +Returns a [PropertyLValue](propertylvalue.md) or sets an indexed property or array element. |
| 182 | + |
| 183 | +```cpp |
| 184 | +Value Napi::Object::operator[] (const char* utf8name) const; |
| 185 | +``` |
| 186 | +- `[in] utf8name`: UTF-8 encoded null-terminated property name. |
| 187 | + |
| 188 | +Returns the named property as a [Value](value.md). |
| 189 | + |
| 190 | +```cpp |
| 191 | +Value Napi::Object::operator[] (const std::string& utf8name) const; |
| 192 | +``` |
| 193 | +- `[in] utf8name`: UTF-8 encoded property name. |
| 194 | + |
| 195 | +Returns the named property as a [Value](value.md). |
| 196 | + |
| 197 | +```cpp |
| 198 | +Value Napi::Object::operator[] (uint32_t index) const; |
| 199 | +``` |
| 200 | +- `[in] index`: Element index. |
| 201 | + |
| 202 | +Returns an indexed property or array element as a [Value](value.md). |
0 commit comments