|
1 |
| -# property descriptor |
| 1 | +# Property Descriptor |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Example |
| 6 | + |
| 7 | +```cpp |
| 8 | +#include <napi.h> |
| 9 | + |
| 10 | +using namespace Napi; |
| 11 | + |
| 12 | +Value TestGetter(const CallbackInfo& info) { |
| 13 | + return Boolean::New(info.Env(), testValue); |
| 14 | +} |
| 15 | + |
| 16 | +void TestSetter(const CallbackInfo& info) { |
| 17 | + testValue = info[0].As<Boolean>(); |
| 18 | +} |
| 19 | + |
| 20 | +Value TestFunction(const CallbackInfo& info) { |
| 21 | + return Boolean::New(info.Env(), true); |
| 22 | +} |
| 23 | + |
| 24 | +Void Init(Env env) { |
| 25 | + // Accessor |
| 26 | + PropertyDescriptor pd1 = PropertyDescriptor::Accessor("pd1", TestGetter); |
| 27 | + PropertyDescriptor pd2 = PropertyDescriptor::Accessor("pd2", TestGetter, TestSetter); |
| 28 | + |
| 29 | + // Function |
| 30 | + PropertyDescriptor pd3 = PropertyDescriptor::Function("function", TestFunction); |
| 31 | + |
| 32 | + // Value |
| 33 | + Boolean true_bool = Boolean::New(env, true); |
| 34 | + PropertyDescriptor pd4 = PropertyDescriptor::Value("boolean value", TestFunction, napi_writable); |
| 35 | + |
| 36 | + // Assign to an Object |
| 37 | + Object obj = Object::New(env); |
| 38 | + obj.DefineProperties({pd1, pd2, pd3, pd4}); |
| 39 | +} |
| 40 | +``` |
| 41 | +
|
| 42 | +## Methods |
| 43 | +
|
| 44 | +### Constructor |
| 45 | +
|
| 46 | +```cpp |
| 47 | +Napi::PropertyDescriptor::PropertyDescriptor (napi_property_descriptor desc); |
| 48 | +``` |
| 49 | + |
| 50 | +* `[in] desc`: A PropertyDescriptor that is needed in order to create another PropertyDescriptor. |
| 51 | + |
| 52 | +### Accessor |
| 53 | + |
| 54 | +```cpp |
| 55 | +static PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, |
| 56 | + Getter getter, |
| 57 | + napi_property_attributes attributes = napi_default, |
| 58 | + void *data = nullptr); |
| 59 | +``` |
| 60 | +
|
| 61 | +* `[in] name`: The name used for the getter function. |
| 62 | +* `[in] getter`: A getter function. |
| 63 | +* `[in] attributes`: Potential attributes for the getter function. |
| 64 | +* `[in] data`: A pointer to data of any type, default is a null pointer. |
| 65 | +
|
| 66 | +Returns a PropertyDescriptor that contains a function. |
| 67 | +
|
| 68 | +The name of the property can be any of the following types: |
| 69 | +- `const char*` |
| 70 | +- `const std::string &` |
| 71 | +- `napi_value value` |
| 72 | +- `Name` |
| 73 | +
|
| 74 | +```cpp |
| 75 | +static PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, |
| 76 | + Getter getter, |
| 77 | + Setter setter, |
| 78 | + napi_property_attributes attributes = napi_default, |
| 79 | + void *data = nullptr); |
| 80 | +``` |
| 81 | + |
| 82 | +* `[in] name`: The name of the getter and setter function. |
| 83 | +* `[in] getter`: The getter function. |
| 84 | +* `[in] setter`: The setter function. |
| 85 | +* `[in] attributes`: Potential attributes for the getter function. |
| 86 | +* `[in] data`: A pointer to data of any type, default is a null pointer. |
| 87 | + |
| 88 | +Returns a PropertyDescriptor that contains a Getter and Setter function. |
| 89 | + |
| 90 | +The name of the property can be any of the following types: |
| 91 | +- `const char*` |
| 92 | +- `const std::string &` |
| 93 | +- `napi_value value` |
| 94 | +- `Name` |
| 95 | + |
| 96 | +### Function |
| 97 | + |
| 98 | +```cpp |
| 99 | +static PropertyDescriptor Napi::PropertyDescriptor::Function (___ name, |
| 100 | + Callable cb, |
| 101 | + napi_property_attributes attributes = napi_default, |
| 102 | + void *data = nullptr); |
| 103 | +``` |
| 104 | +
|
| 105 | +* `[in] name`: The name of the Callable function. |
| 106 | +* `[in] cb`: The function |
| 107 | +* `[in] attributes`: Potential attributes for the getter function. |
| 108 | +* `[in] data`: A pointer to data of any type, default is a null pointer. |
| 109 | +
|
| 110 | +Returns a PropertyDescriptor that contains a callable Function. |
| 111 | +
|
| 112 | +The name of the property can be any of the following types: |
| 113 | +- `const char*` |
| 114 | +- `const std::string &` |
| 115 | +- `napi_value value` |
| 116 | +- `Name` |
| 117 | +
|
| 118 | +### Value |
| 119 | +
|
| 120 | +```cpp |
| 121 | +static PropertyDescriptor Napi::PropertyDescriptor::Value (___ name, |
| 122 | + napi_value value, |
| 123 | + napi_property_attributes attributes = napi_default); |
| 124 | +``` |
| 125 | + |
| 126 | +The name of the property can be any of the following types: |
| 127 | +- `const char*` |
| 128 | +- `const std::string &` |
| 129 | +- `napi_value value` |
| 130 | +- `Name` |
| 131 | + |
| 132 | +## Related Information |
| 133 | + |
| 134 | +### napi\_property\_attributes |
| 135 | +`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: |
| 136 | +- napi\_default, |
| 137 | +- napi\_writable, |
| 138 | +- napi\_enumerable, |
| 139 | +- napi\_configurable |
| 140 | +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). |
2 | 141 |
|
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/) |
|
0 commit comments