Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Object documentation #254

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 200 additions & 3 deletions doc/object.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,202 @@
# Object

You are reading a draft of the next documentation and it's in continuos update so
if you don't find what you need please refer to:
[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/)
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:

- [Value](value.md) and extends [Array](array.md)
- [ArrayBuffer](array_buffer.md)
- [Buffer<T>](buffer.md)
- [Function](function.md)
- [TypedArray](typed_array.md).

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().

## Example
```cpp
#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(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(42);
Value val3 = obj.Get("Douglas Adams");

// Test if objects have properties.
bool obj1 = obj.Has("hello"); // true
bool obj2 = obj.Has("world"); // false

}
```

## Methods

### Empty Constructor

```cpp
Napi::Object::Object();
```
Creates a new empty Object instance.

### Constructor

```cpp
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:
- bool
- Any integer type
- Any floating point type
- const char* (encoded using UTF-8, null-terminated)
- const char16_t* (encoded using UTF-16-LE, null-terminated)
- std::string (encoded using UTF-8)
- std::u16string
- napi::Value
- napi_value

Creates a non-empty Object instance.

### New()

```cpp
Object Napi::Object::New(napi_env env);
```
- `[in] env`: The `napi_env` environment in which to construct the Value object.

Creates a new Object value.

### Set()

```cpp
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_value`
- [Value](value.md)
- `const char*`
- `const std::string&`
- `uint32_t`

While the value must be any of the following types:
- `napi_value`
- [Value](value.md)
- `const char*`
- `std::string&`
- `bool`
- `double`

### Get()

```cpp
Value Napi::Object::Get(____ key);
```
- `[in] key`: The name of the property to return the value for.

Returns the [Value](value.md) associated with the key property. Returns NULL if no such key exists.

The `key` can be any of the following types:
- `napi_value`
- [Value](value.md)
- `const char *`
- `const std::string &`
- `uint32_t`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should say what it returns if the property does not exist.

### Has()

```cpp
bool 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.

### InstanceOf()

```cpp
bool Napi::Object::InstanceOf (const Function& constructor) const
```
- `[in] constructor`: The constructor [Function](function.md) of the value that is being compared with the object.

Returns a `bool` that is true if the Object is an instance created by the `constructor` and false otherwise.

Note: This is equivalent to the JavaScript instanceof operator.

### DefineProperty()

```cpp
void Napi::Object::DefineProperty (const PropertyDescriptor& property);
```
- `[in] property`: A [PropertyDescriptor](propertydescriptor.md).

Define a property on the object.

### DefineProperties()

```cpp
void Napi::Object::DefineProperties (____ properties)
```
- `[in] properties`: A list of [PropertyDescriptor](propertydescriptor.md). Can be one of the following types:
- const std::initializer_list<PropertyDescriptor>&
- const std::vector<PropertyDescriptor>&

Defines properties on the object.

### Operator[]()

```cpp
PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some examples in the introduction which shows using these might help .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to use these myself, do you know of any existing examples I can take a look at?

```
- `[in] utf8name`: UTF-8 encoded null-terminated property name.

Returns a [PropertyLValue](propertylvalue.md) as the named property or sets the named property.

```cpp
PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name);
```
- `[in] utf8name`: UTF-8 encoded property name.

Returns a [PropertyLValue](propertylvalue.md) as the named property or sets the named property.

```cpp
PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index);
```
- `[in] index`: Element index.

Returns a [PropertyLValue](propertylvalue.md) or sets an indexed property or array element.

```cpp
Value Napi::Object::operator[] (const char* utf8name) const;
```
- `[in] utf8name`: UTF-8 encoded null-terminated property name.

Returns the named property as a [Value](value.md).

```cpp
Value Napi::Object::operator[] (const std::string& utf8name) const;
```
- `[in] utf8name`: UTF-8 encoded property name.

Returns the named property as a [Value](value.md).

```cpp
Value Napi::Object::operator[] (uint32_t index) const;
```
- `[in] index`: Element index.

Returns an indexed property or array element as a [Value](value.md).