-
Notifications
You must be signed in to change notification settings - Fork 465
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
PropertyDescriptor documentation #309
Closed
+140
−4
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,141 @@ | ||
# property descriptor | ||
# Property Descriptor | ||
|
||
An [Object](object.md) can be assigned properites via its [DefineProperty](object.md#defineproperty) and [DefineProperties](object.md#defineproperties) function, which take PropertyDescrptor(s) as their parameters. The PropertyDescriptor can contain either values or functions, which are then assigned to the Object. Note that a single instance of a PropertyDescriptor class can only contain either one value, or at most two functions. PropertyDescriptors can only be created through the class methods [Accessor](#accessor), [Function](#function), or [Value](#value), each of which return a new static instance of a PropertyDescriptor. | ||
|
||
## Example | ||
|
||
```cpp | ||
#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) { | ||
// Accessor | ||
PropertyDescriptor pd1 = PropertyDescriptor::Accessor("pd1", TestGetter); | ||
PropertyDescriptor pd2 = PropertyDescriptor::Accessor("pd2", TestGetter, TestSetter); | ||
|
||
// Function | ||
PropertyDescriptor pd3 = PropertyDescriptor::Function("function", TestFunction); | ||
|
||
// Value | ||
Boolean true_bool = Boolean::New(env, true); | ||
PropertyDescriptor pd4 = PropertyDescriptor::Value("boolean value", TestFunction, napi_writable); | ||
|
||
// Assign to an Object | ||
Object obj = Object::New(env); | ||
obj.DefineProperties({pd1, pd2, pd3, pd4}); | ||
} | ||
``` | ||
|
||
## Methods | ||
|
||
### Constructor | ||
|
||
```cpp | ||
Napi::PropertyDescriptor::PropertyDescriptor (napi_property_descriptor desc); | ||
``` | ||
|
||
* `[in] desc`: A PropertyDescriptor that is needed in order to create another PropertyDescriptor. | ||
|
||
### Accessor | ||
|
||
```cpp | ||
static PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, | ||
Getter getter, | ||
napi_property_attributes attributes = napi_default, | ||
void *data = nullptr); | ||
``` | ||
|
||
* `[in] name`: The name used for the getter function. | ||
* `[in] getter`: A getter function. | ||
* `[in] attributes`: Potential attributes for the getter function. | ||
* `[in] data`: A pointer to data of any type, default is a null pointer. | ||
|
||
Returns a PropertyDescriptor that contains a function. | ||
|
||
The name of the property can be any of the following types: | ||
- `const char*` | ||
- `const std::string &` | ||
- `napi_value value` | ||
- `Name` | ||
|
||
```cpp | ||
static PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, | ||
Getter getter, | ||
Setter setter, | ||
napi_property_attributes attributes = napi_default, | ||
void *data = nullptr); | ||
``` | ||
|
||
* `[in] name`: The name of the getter and setter function. | ||
* `[in] getter`: The getter function. | ||
* `[in] setter`: The setter function. | ||
* `[in] attributes`: Potential attributes for the getter function. | ||
* `[in] data`: A pointer to data of any type, default is a null pointer. | ||
|
||
Returns a PropertyDescriptor that contains a Getter and Setter function. | ||
|
||
The name of the property can be any of the following types: | ||
- `const char*` | ||
- `const std::string &` | ||
- `napi_value value` | ||
- `Name` | ||
|
||
### Function | ||
|
||
```cpp | ||
static PropertyDescriptor Napi::PropertyDescriptor::Function (___ name, | ||
Callable cb, | ||
napi_property_attributes attributes = napi_default, | ||
void *data = nullptr); | ||
``` | ||
|
||
* `[in] name`: The name of the Callable function. | ||
* `[in] cb`: The function | ||
* `[in] attributes`: Potential attributes for the getter function. | ||
* `[in] data`: A pointer to data of any type, default is a null pointer. | ||
|
||
Returns a PropertyDescriptor that contains a callable Function. | ||
|
||
The name of the property can be any of the following types: | ||
- `const char*` | ||
- `const std::string &` | ||
- `napi_value value` | ||
- `Name` | ||
|
||
### Value | ||
|
||
```cpp | ||
static PropertyDescriptor Napi::PropertyDescriptor::Value (___ name, | ||
napi_value value, | ||
napi_property_attributes attributes = napi_default); | ||
``` | ||
|
||
The name of the property can be any of the following types: | ||
- `const char*` | ||
- `const std::string &` | ||
- `napi_value value` | ||
- `Name` | ||
|
||
## Related Information | ||
|
||
### napi\_property\_attributes | ||
`napi_property_attributes` are flags used to indicate to JavaScript certain permissions that the property is meant to have. The following are the flag options: | ||
- napi\_default, | ||
- napi\_writable, | ||
- napi\_enumerable, | ||
- napi\_configurable | ||
For more information on the flags and on napi\_property\_attributes, please read the documentation [here](https://github.com/nodejs/node/blob/master/doc/api/n-api.md#napi_property_attributes). | ||
|
||
You are reading a draft of the next documentation and it's in continuous 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/) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to fix alignment spacing here.