18c339a94Sopenharmony_ci# Object Wrap 28c339a94Sopenharmony_ci 38c339a94Sopenharmony_ciClass `Napi::ObjectWrap<T>` inherits from class [`Napi::InstanceWrap<T>`][]. 48c339a94Sopenharmony_ci 58c339a94Sopenharmony_ciThe `Napi::ObjectWrap<T>` class is used to bind the lifetime of C++ code to a 68c339a94Sopenharmony_ciJavaScript object. Once bound, each time an instance of the JavaScript object 78c339a94Sopenharmony_ciis created, an instance of the C++ class will also be created. When a method 88c339a94Sopenharmony_ciis called on the JavaScript object which is defined as an InstanceMethod, the 98c339a94Sopenharmony_cicorresponding C++ method on the wrapped C++ class will be invoked. 108c339a94Sopenharmony_ci 118c339a94Sopenharmony_ciIn order to create a wrapper it's necessary to extend the 128c339a94Sopenharmony_ci`Napi::ObjectWrap<T>` class which contains all the plumbing to connect 138c339a94Sopenharmony_ciJavaScript code with a C++ object. Classes extending `Napi::ObjectWrap` can be 148c339a94Sopenharmony_ciinstantiated from JavaScript using the **new** operator, and their methods can 158c339a94Sopenharmony_cibe directly invoked from JavaScript. The **wrap** word refers to a way of 168c339a94Sopenharmony_cigrouping methods and state of the class because it will be necessary write 178c339a94Sopenharmony_cicustom code to bridge each of your C++ class methods. 188c339a94Sopenharmony_ci 198c339a94Sopenharmony_ci**Caution:** When the JavaScript object is garbage collected, the call to the 208c339a94Sopenharmony_ciC++ destructor may be deferred until a later time. Within that period, 218c339a94Sopenharmony_ci`Value()` will return an empty value. 228c339a94Sopenharmony_ci 238c339a94Sopenharmony_ci## Example 248c339a94Sopenharmony_ci 258c339a94Sopenharmony_ci```cpp 268c339a94Sopenharmony_ci#include <napi.h> 278c339a94Sopenharmony_ci 288c339a94Sopenharmony_ciclass Example : public Napi::ObjectWrap<Example> { 298c339a94Sopenharmony_ci public: 308c339a94Sopenharmony_ci static Napi::Object Init(Napi::Env env, Napi::Object exports); 318c339a94Sopenharmony_ci Example(const Napi::CallbackInfo& info); 328c339a94Sopenharmony_ci static Napi::Value CreateNewItem(const Napi::CallbackInfo& info); 338c339a94Sopenharmony_ci 348c339a94Sopenharmony_ci private: 358c339a94Sopenharmony_ci double _value; 368c339a94Sopenharmony_ci Napi::Value GetValue(const Napi::CallbackInfo& info); 378c339a94Sopenharmony_ci Napi::Value SetValue(const Napi::CallbackInfo& info); 388c339a94Sopenharmony_ci}; 398c339a94Sopenharmony_ci 408c339a94Sopenharmony_ciNapi::Object Example::Init(Napi::Env env, Napi::Object exports) { 418c339a94Sopenharmony_ci // This method is used to hook the accessor and method callbacks 428c339a94Sopenharmony_ci Napi::Function func = DefineClass(env, "Example", { 438c339a94Sopenharmony_ci InstanceMethod<&Example::GetValue>("GetValue", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), 448c339a94Sopenharmony_ci InstanceMethod<&Example::SetValue>("SetValue", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), 458c339a94Sopenharmony_ci StaticMethod<&Example::CreateNewItem>("CreateNewItem", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), 468c339a94Sopenharmony_ci }); 478c339a94Sopenharmony_ci 488c339a94Sopenharmony_ci Napi::FunctionReference* constructor = new Napi::FunctionReference(); 498c339a94Sopenharmony_ci 508c339a94Sopenharmony_ci // Create a persistent reference to the class constructor. This will allow 518c339a94Sopenharmony_ci // a function called on a class prototype and a function 528c339a94Sopenharmony_ci // called on instance of a class to be distinguished from each other. 538c339a94Sopenharmony_ci *constructor = Napi::Persistent(func); 548c339a94Sopenharmony_ci exports.Set("Example", func); 558c339a94Sopenharmony_ci 568c339a94Sopenharmony_ci // Store the constructor as the add-on instance data. This will allow this 578c339a94Sopenharmony_ci // add-on to support multiple instances of itself running on multiple worker 588c339a94Sopenharmony_ci // threads, as well as multiple instances of itself running in different 598c339a94Sopenharmony_ci // contexts on the same thread. 608c339a94Sopenharmony_ci // 618c339a94Sopenharmony_ci // By default, the value set on the environment here will be destroyed when 628c339a94Sopenharmony_ci // the add-on is unloaded using the `delete` operator, but it is also 638c339a94Sopenharmony_ci // possible to supply a custom deleter. 648c339a94Sopenharmony_ci env.SetInstanceData<Napi::FunctionReference>(constructor); 658c339a94Sopenharmony_ci 668c339a94Sopenharmony_ci return exports; 678c339a94Sopenharmony_ci} 688c339a94Sopenharmony_ci 698c339a94Sopenharmony_ciExample::Example(const Napi::CallbackInfo& info) : 708c339a94Sopenharmony_ci Napi::ObjectWrap<Example>(info) { 718c339a94Sopenharmony_ci Napi::Env env = info.Env(); 728c339a94Sopenharmony_ci // ... 738c339a94Sopenharmony_ci Napi::Number value = info[0].As<Napi::Number>(); 748c339a94Sopenharmony_ci this->_value = value.DoubleValue(); 758c339a94Sopenharmony_ci} 768c339a94Sopenharmony_ci 778c339a94Sopenharmony_ciNapi::Value Example::GetValue(const Napi::CallbackInfo& info){ 788c339a94Sopenharmony_ci Napi::Env env = info.Env(); 798c339a94Sopenharmony_ci return Napi::Number::New(env, this->_value); 808c339a94Sopenharmony_ci} 818c339a94Sopenharmony_ci 828c339a94Sopenharmony_ciNapi::Value Example::SetValue(const Napi::CallbackInfo& info){ 838c339a94Sopenharmony_ci Napi::Env env = info.Env(); 848c339a94Sopenharmony_ci // ... 858c339a94Sopenharmony_ci Napi::Number value = info[0].As<Napi::Number>(); 868c339a94Sopenharmony_ci this->_value = value.DoubleValue(); 878c339a94Sopenharmony_ci return this->GetValue(info); 888c339a94Sopenharmony_ci} 898c339a94Sopenharmony_ci 908c339a94Sopenharmony_ci// Initialize native add-on 918c339a94Sopenharmony_ciNapi::Object Init (Napi::Env env, Napi::Object exports) { 928c339a94Sopenharmony_ci Example::Init(env, exports); 938c339a94Sopenharmony_ci return exports; 948c339a94Sopenharmony_ci} 958c339a94Sopenharmony_ci 968c339a94Sopenharmony_ci// Create a new item using the constructor stored during Init. 978c339a94Sopenharmony_ciNapi::Value Example::CreateNewItem(const Napi::CallbackInfo& info) { 988c339a94Sopenharmony_ci // Retrieve the instance data we stored during `Init()`. We only stored the 998c339a94Sopenharmony_ci // constructor there, so we retrieve it here to create a new instance of the 1008c339a94Sopenharmony_ci // JS class the constructor represents. 1018c339a94Sopenharmony_ci Napi::FunctionReference* constructor = 1028c339a94Sopenharmony_ci info.Env().GetInstanceData<Napi::FunctionReference>(); 1038c339a94Sopenharmony_ci return constructor->New({ Napi::Number::New(info.Env(), 42) }); 1048c339a94Sopenharmony_ci} 1058c339a94Sopenharmony_ci 1068c339a94Sopenharmony_ci// Register and initialize native add-on 1078c339a94Sopenharmony_ciNODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) 1088c339a94Sopenharmony_ci``` 1098c339a94Sopenharmony_ci 1108c339a94Sopenharmony_ciThe above code can be used from JavaScript as follows: 1118c339a94Sopenharmony_ci 1128c339a94Sopenharmony_ci```js 1138c339a94Sopenharmony_ci'use strict' 1148c339a94Sopenharmony_ci 1158c339a94Sopenharmony_ciconst { Example } = require('bindings')('addon') 1168c339a94Sopenharmony_ci 1178c339a94Sopenharmony_ciconst example = new Example(11) 1188c339a94Sopenharmony_ciconsole.log(example.GetValue()) 1198c339a94Sopenharmony_ci// It prints 11 1208c339a94Sopenharmony_ciexample.SetValue(19) 1218c339a94Sopenharmony_ciconsole.log(example.GetValue()); 1228c339a94Sopenharmony_ci// It prints 19 1238c339a94Sopenharmony_ci``` 1248c339a94Sopenharmony_ci 1258c339a94Sopenharmony_ciAt initialization time, the `Napi::ObjectWrap::DefineClass()` method must be 1268c339a94Sopenharmony_ciused to hook up the accessor and method callbacks. It takes a list of property 1278c339a94Sopenharmony_cidescriptors, which can be constructed via the various static methods on the base 1288c339a94Sopenharmony_ciclass. 1298c339a94Sopenharmony_ci 1308c339a94Sopenharmony_ciWhen JavaScript code invokes the constructor, the constructor callback will 1318c339a94Sopenharmony_cicreate a new C++ instance and "wrap" it into the newly created JavaScript 1328c339a94Sopenharmony_ciobject. 1338c339a94Sopenharmony_ci 1348c339a94Sopenharmony_ciWhen JavaScript code invokes a method or a property accessor on the class the 1358c339a94Sopenharmony_cicorresponding C++ callback function will be executed. 1368c339a94Sopenharmony_ci 1378c339a94Sopenharmony_ciFor a wrapped object it could be difficult to distinguish between a function 1388c339a94Sopenharmony_cicalled on a class prototype and a function called on instance of a class. 1398c339a94Sopenharmony_ciTherefore it is good practice to save a persistent reference to the class 1408c339a94Sopenharmony_ciconstructor. This allows the two cases to be distinguished from each other by 1418c339a94Sopenharmony_cichecking the this object against the class constructor. 1428c339a94Sopenharmony_ci 1438c339a94Sopenharmony_ci## Methods 1448c339a94Sopenharmony_ci 1458c339a94Sopenharmony_ci### Constructor 1468c339a94Sopenharmony_ci 1478c339a94Sopenharmony_ciCreates a new instance of a JavaScript object that wraps native instance. 1488c339a94Sopenharmony_ci 1498c339a94Sopenharmony_ci```cpp 1508c339a94Sopenharmony_ciNapi::ObjectWrap(const Napi::CallbackInfo& callbackInfo); 1518c339a94Sopenharmony_ci``` 1528c339a94Sopenharmony_ci 1538c339a94Sopenharmony_ci- `[in] callbackInfo`: The object representing the components of the JavaScript 1548c339a94Sopenharmony_cirequest being made. 1558c339a94Sopenharmony_ci 1568c339a94Sopenharmony_ci### Unwrap 1578c339a94Sopenharmony_ci 1588c339a94Sopenharmony_ciRetrieves a native instance wrapped in a JavaScript object. 1598c339a94Sopenharmony_ci 1608c339a94Sopenharmony_ci```cpp 1618c339a94Sopenharmony_cistatic T* Napi::ObjectWrap::Unwrap(Napi::Object wrapper); 1628c339a94Sopenharmony_ci``` 1638c339a94Sopenharmony_ci 1648c339a94Sopenharmony_ci* `[in] wrapper`: The JavaScript object that wraps the native instance. 1658c339a94Sopenharmony_ci 1668c339a94Sopenharmony_ciReturns a native instance wrapped in a JavaScript object. Given the 1678c339a94Sopenharmony_ci`Napi::Object`, this allows a method to get a pointer to the wrapped 1688c339a94Sopenharmony_ciC++ object and then reference fields, call methods, etc. within that class. 1698c339a94Sopenharmony_ciIn many cases calling Unwrap is not required, as methods can 1708c339a94Sopenharmony_ciuse the `this` field for ObjectWrap when running in a method on a 1718c339a94Sopenharmony_ciclass that extends ObjectWrap. 1728c339a94Sopenharmony_ci 1738c339a94Sopenharmony_ci### DefineClass 1748c339a94Sopenharmony_ci 1758c339a94Sopenharmony_ciDefnines a JavaScript class with constructor, static and instance properties and 1768c339a94Sopenharmony_cimethods. 1778c339a94Sopenharmony_ci 1788c339a94Sopenharmony_ci```cpp 1798c339a94Sopenharmony_cistatic Napi::Function Napi::ObjectWrap::DefineClass(Napi::Env env, 1808c339a94Sopenharmony_ci const char* utf8name, 1818c339a94Sopenharmony_ci const std::initializer_list<PropertyDescriptor>& properties, 1828c339a94Sopenharmony_ci void* data = nullptr); 1838c339a94Sopenharmony_ci``` 1848c339a94Sopenharmony_ci 1858c339a94Sopenharmony_ci* `[in] env`: The environment in which to construct a JavaScript class. 1868c339a94Sopenharmony_ci* `[in] utf8name`: Null-terminated string that represents the name of the 1878c339a94Sopenharmony_ciJavaScript constructor function. 1888c339a94Sopenharmony_ci* `[in] properties`: Initializer list of class property descriptor describing 1898c339a94Sopenharmony_cistatic and instance properties and methods of the class. 1908c339a94Sopenharmony_ciSee: [`Class property and descriptor`](class_property_descriptor.md). 1918c339a94Sopenharmony_ci* `[in] data`: User-provided data passed to the constructor callback as `data` 1928c339a94Sopenharmony_ciproperty of the `Napi::CallbackInfo`. 1938c339a94Sopenharmony_ci 1948c339a94Sopenharmony_ciReturns a `Napi::Function` representing the constructor function for the class. 1958c339a94Sopenharmony_ci 1968c339a94Sopenharmony_ci### DefineClass 1978c339a94Sopenharmony_ci 1988c339a94Sopenharmony_ciDefnines a JavaScript class with constructor, static and instance properties and 1998c339a94Sopenharmony_cimethods. 2008c339a94Sopenharmony_ci 2018c339a94Sopenharmony_ci```cpp 2028c339a94Sopenharmony_cistatic Napi::Function Napi::ObjectWrap::DefineClass(Napi::Env env, 2038c339a94Sopenharmony_ci const char* utf8name, 2048c339a94Sopenharmony_ci const std::vector<PropertyDescriptor>& properties, 2058c339a94Sopenharmony_ci void* data = nullptr); 2068c339a94Sopenharmony_ci``` 2078c339a94Sopenharmony_ci 2088c339a94Sopenharmony_ci* `[in] env`: The environment in which to construct a JavaScript class. 2098c339a94Sopenharmony_ci* `[in] utf8name`: Null-terminated string that represents the name of the 2108c339a94Sopenharmony_ciJavaScript constructor function. 2118c339a94Sopenharmony_ci* `[in] properties`: Vector of class property descriptor describing static and 2128c339a94Sopenharmony_ciinstance properties and methods of the class. 2138c339a94Sopenharmony_ciSee: [`Class property and descriptor`](class_property_descriptor.md). 2148c339a94Sopenharmony_ci* `[in] data`: User-provided data passed to the constructor callback as `data` 2158c339a94Sopenharmony_ciproperty of the `Napi::CallbackInfo`. 2168c339a94Sopenharmony_ci 2178c339a94Sopenharmony_ciReturns a `Napi::Function` representing the constructor function for the class. 2188c339a94Sopenharmony_ci 2198c339a94Sopenharmony_ci### OnCalledAsFunction 2208c339a94Sopenharmony_ci 2218c339a94Sopenharmony_ciProvides an opportunity to customize the behavior when a `Napi::ObjectWrap<T>` 2228c339a94Sopenharmony_ciclass is called from JavaScript as a function (without the **new** operator). 2238c339a94Sopenharmony_ci 2248c339a94Sopenharmony_ciThe default behavior in this scenario is to throw a `Napi::TypeError` with the 2258c339a94Sopenharmony_cimessage `Class constructors cannot be invoked without 'new'`. Define this 2268c339a94Sopenharmony_cipublic method on your derived class to override that behavior. 2278c339a94Sopenharmony_ci 2288c339a94Sopenharmony_ciFor example, you could internally re-call the JavaScript contstructor _with_ 2298c339a94Sopenharmony_cithe **new** operator (via 2308c339a94Sopenharmony_ci`Napi::Function::New(const std::vector<napi_value> &args)`), and return the 2318c339a94Sopenharmony_ciresulting object. Or you might do something else entirely, such as the way 2328c339a94Sopenharmony_ci[`Date()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#constructor) 2338c339a94Sopenharmony_ciproduces a string when called as a function. 2348c339a94Sopenharmony_ci 2358c339a94Sopenharmony_ci```cpp 2368c339a94Sopenharmony_cistatic Napi::Value OnCalledAsFunction(const Napi::CallbackInfo& callbackInfo); 2378c339a94Sopenharmony_ci``` 2388c339a94Sopenharmony_ci 2398c339a94Sopenharmony_ci- `[in] callbackInfo`: The object representing the components of the JavaScript 2408c339a94Sopenharmony_cirequest being made. 2418c339a94Sopenharmony_ci 2428c339a94Sopenharmony_ci### Finalize 2438c339a94Sopenharmony_ci 2448c339a94Sopenharmony_ciProvides an opportunity to run cleanup code that requires access to the 2458c339a94Sopenharmony_ci`Napi::Env` before the wrapped native object instance is freed. Override to 2468c339a94Sopenharmony_ciimplement. 2478c339a94Sopenharmony_ci 2488c339a94Sopenharmony_ci```cpp 2498c339a94Sopenharmony_civirtual void Finalize(Napi::Env env); 2508c339a94Sopenharmony_ci``` 2518c339a94Sopenharmony_ci 2528c339a94Sopenharmony_ci- `[in] env`: `Napi::Env`. 2538c339a94Sopenharmony_ci 2548c339a94Sopenharmony_ci### StaticMethod 2558c339a94Sopenharmony_ci 2568c339a94Sopenharmony_ciCreates property descriptor that represents a static method of a JavaScript 2578c339a94Sopenharmony_ciclass. 2588c339a94Sopenharmony_ci 2598c339a94Sopenharmony_ci```cpp 2608c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod( 2618c339a94Sopenharmony_ci const char* utf8name, 2628c339a94Sopenharmony_ci StaticVoidMethodCallback method, 2638c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 2648c339a94Sopenharmony_ci void* data = nullptr); 2658c339a94Sopenharmony_ci``` 2668c339a94Sopenharmony_ci 2678c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string that represents the name of a static 2688c339a94Sopenharmony_cimethod for the class. 2698c339a94Sopenharmony_ci- `[in] method`: The native function that represents a static method of a 2708c339a94Sopenharmony_ciJavaScript class. 2718c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 2728c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 2738c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into method when it is invoked. 2748c339a94Sopenharmony_ci 2758c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents the static method of a 2768c339a94Sopenharmony_ciJavaScript class. 2778c339a94Sopenharmony_ci 2788c339a94Sopenharmony_ci### StaticMethod 2798c339a94Sopenharmony_ci 2808c339a94Sopenharmony_ciCreates property descriptor that represents a static method of a JavaScript 2818c339a94Sopenharmony_ciclass. 2828c339a94Sopenharmony_ci 2838c339a94Sopenharmony_ci```cpp 2848c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod( 2858c339a94Sopenharmony_ci const char* utf8name, 2868c339a94Sopenharmony_ci StaticMethodCallback method, 2878c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 2888c339a94Sopenharmony_ci void* data = nullptr); 2898c339a94Sopenharmony_ci``` 2908c339a94Sopenharmony_ci 2918c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string that represents the name of a static 2928c339a94Sopenharmony_cimethod for the class. 2938c339a94Sopenharmony_ci- `[in] method`: The native function that represents a static method of a 2948c339a94Sopenharmony_ciJavaScript class. 2958c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 2968c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 2978c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into method when it is invoked. 2988c339a94Sopenharmony_ci 2998c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents a static method of a 3008c339a94Sopenharmony_ciJavaScript class. 3018c339a94Sopenharmony_ci 3028c339a94Sopenharmony_ci### StaticMethod 3038c339a94Sopenharmony_ci 3048c339a94Sopenharmony_ciCreates property descriptor that represents a static method of a JavaScript 3058c339a94Sopenharmony_ciclass. 3068c339a94Sopenharmony_ci 3078c339a94Sopenharmony_ci```cpp 3088c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name, 3098c339a94Sopenharmony_ci StaticVoidMethodCallback method, 3108c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 3118c339a94Sopenharmony_ci void* data = nullptr); 3128c339a94Sopenharmony_ci``` 3138c339a94Sopenharmony_ci 3148c339a94Sopenharmony_ci- `[in] name`: Napi::Symbol that represents the name of a static 3158c339a94Sopenharmony_cimethod for the class. 3168c339a94Sopenharmony_ci- `[in] method`: The native function that represents a static method of a 3178c339a94Sopenharmony_ciJavaScript class. 3188c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 3198c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 3208c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into method when it is invoked. 3218c339a94Sopenharmony_ci 3228c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents the static method of a 3238c339a94Sopenharmony_ciJavaScript class. 3248c339a94Sopenharmony_ci 3258c339a94Sopenharmony_ci### StaticMethod 3268c339a94Sopenharmony_ci 3278c339a94Sopenharmony_ciCreates property descriptor that represents a static method of a JavaScript 3288c339a94Sopenharmony_ciclass. 3298c339a94Sopenharmony_ci 3308c339a94Sopenharmony_ci```cpp 3318c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name, 3328c339a94Sopenharmony_ci StaticMethodCallback method, 3338c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 3348c339a94Sopenharmony_ci void* data = nullptr); 3358c339a94Sopenharmony_ci``` 3368c339a94Sopenharmony_ci 3378c339a94Sopenharmony_cimethod for the class. 3388c339a94Sopenharmony_ci- `[in] name`: Napi::Symbol that represents the name of a static. 3398c339a94Sopenharmony_ci- `[in] method`: The native function that represents a static method of a 3408c339a94Sopenharmony_ciJavaScript class. 3418c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 3428c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 3438c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into method when it is invoked. 3448c339a94Sopenharmony_ci 3458c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents a static method of a 3468c339a94Sopenharmony_ciJavaScript class. 3478c339a94Sopenharmony_ci 3488c339a94Sopenharmony_ci### StaticMethod 3498c339a94Sopenharmony_ci 3508c339a94Sopenharmony_ciCreates property descriptor that represents a static method of a JavaScript 3518c339a94Sopenharmony_ciclass. 3528c339a94Sopenharmony_ci 3538c339a94Sopenharmony_ci```cpp 3548c339a94Sopenharmony_citemplate <StaticVoidMethodCallback method> 3558c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod( 3568c339a94Sopenharmony_ci const char* utf8name, 3578c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 3588c339a94Sopenharmony_ci void* data = nullptr); 3598c339a94Sopenharmony_ci``` 3608c339a94Sopenharmony_ci 3618c339a94Sopenharmony_ci- `[in] method`: The native function that represents a static method of a 3628c339a94Sopenharmony_ciJavaScript class. This function returns nothing. 3638c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string that represents the name of a static 3648c339a94Sopenharmony_cimethod for the class. 3658c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 3668c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 3678c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into method when it is invoked. 3688c339a94Sopenharmony_ci 3698c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents the static method of a 3708c339a94Sopenharmony_ciJavaScript class. 3718c339a94Sopenharmony_ci 3728c339a94Sopenharmony_ci### StaticMethod 3738c339a94Sopenharmony_ci 3748c339a94Sopenharmony_ciCreates property descriptor that represents a static method of a JavaScript 3758c339a94Sopenharmony_ciclass. 3768c339a94Sopenharmony_ci 3778c339a94Sopenharmony_ci```cpp 3788c339a94Sopenharmony_citemplate <StaticMethodCallback method> 3798c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod( 3808c339a94Sopenharmony_ci const char* utf8name, 3818c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 3828c339a94Sopenharmony_ci void* data = nullptr); 3838c339a94Sopenharmony_ci``` 3848c339a94Sopenharmony_ci 3858c339a94Sopenharmony_ci- `[in] method`: The native function that represents a static method of a 3868c339a94Sopenharmony_ciJavaScript class. 3878c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string that represents the name of a static 3888c339a94Sopenharmony_cimethod for the class. 3898c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 3908c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 3918c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into method when it is invoked. 3928c339a94Sopenharmony_ci 3938c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents a static method of a 3948c339a94Sopenharmony_ciJavaScript class. 3958c339a94Sopenharmony_ci 3968c339a94Sopenharmony_ci### StaticMethod 3978c339a94Sopenharmony_ci 3988c339a94Sopenharmony_ciCreates property descriptor that represents a static method of a JavaScript 3998c339a94Sopenharmony_ciclass. 4008c339a94Sopenharmony_ci 4018c339a94Sopenharmony_ci```cpp 4028c339a94Sopenharmony_citemplate <StaticVoidMethodCallback method> 4038c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name, 4048c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 4058c339a94Sopenharmony_ci void* data = nullptr); 4068c339a94Sopenharmony_ci``` 4078c339a94Sopenharmony_ci 4088c339a94Sopenharmony_ci- `[in] method`: The native function that represents a static method of a 4098c339a94Sopenharmony_ciJavaScript class. 4108c339a94Sopenharmony_ci- `[in] name`: Napi::Symbol that represents the name of a static 4118c339a94Sopenharmony_cimethod for the class. 4128c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 4138c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 4148c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into method when it is invoked. 4158c339a94Sopenharmony_ci 4168c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents the static method of a 4178c339a94Sopenharmony_ciJavaScript class. 4188c339a94Sopenharmony_ci 4198c339a94Sopenharmony_ci### StaticMethod 4208c339a94Sopenharmony_ci 4218c339a94Sopenharmony_ciCreates property descriptor that represents a static method of a JavaScript 4228c339a94Sopenharmony_ciclass. 4238c339a94Sopenharmony_ci 4248c339a94Sopenharmony_ci```cpp 4258c339a94Sopenharmony_citemplate <StaticMethodCallback method> 4268c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name, 4278c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 4288c339a94Sopenharmony_ci void* data = nullptr); 4298c339a94Sopenharmony_ci``` 4308c339a94Sopenharmony_ci 4318c339a94Sopenharmony_ci- `[in] method`: The native function that represents a static method of a 4328c339a94Sopenharmony_ciJavaScript class. 4338c339a94Sopenharmony_ci- `[in] name`: Napi::Symbol that represents the name of a static. 4348c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 4358c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 4368c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into method when it is invoked. 4378c339a94Sopenharmony_ci 4388c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents a static method of a 4398c339a94Sopenharmony_ciJavaScript class. 4408c339a94Sopenharmony_ci 4418c339a94Sopenharmony_ci### StaticAccessor 4428c339a94Sopenharmony_ci 4438c339a94Sopenharmony_ciCreates property descriptor that represents a static accessor property of a 4448c339a94Sopenharmony_ciJavaScript class. 4458c339a94Sopenharmony_ci 4468c339a94Sopenharmony_ci```cpp 4478c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor( 4488c339a94Sopenharmony_ci const char* utf8name, 4498c339a94Sopenharmony_ci StaticGetterCallback getter, 4508c339a94Sopenharmony_ci StaticSetterCallback setter, 4518c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 4528c339a94Sopenharmony_ci void* data = nullptr); 4538c339a94Sopenharmony_ci``` 4548c339a94Sopenharmony_ci 4558c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string that represents the name of a static 4568c339a94Sopenharmony_ciaccessor property for the class. 4578c339a94Sopenharmony_ci- `[in] getter`: The native function to call when a get access to the property 4588c339a94Sopenharmony_ciof a JavaScript class is performed. 4598c339a94Sopenharmony_ci- `[in] setter`: The native function to call when a set access to the property 4608c339a94Sopenharmony_ciof a JavaScript class is performed. 4618c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 4628c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 4638c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into getter or setter when 4648c339a94Sopenharmony_ciis invoked. 4658c339a94Sopenharmony_ci 4668c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents a static accessor 4678c339a94Sopenharmony_ciproperty of a JavaScript class. 4688c339a94Sopenharmony_ci 4698c339a94Sopenharmony_ci### StaticAccessor 4708c339a94Sopenharmony_ci 4718c339a94Sopenharmony_ciCreates property descriptor that represents a static accessor property of a 4728c339a94Sopenharmony_ciJavaScript class. 4738c339a94Sopenharmony_ci 4748c339a94Sopenharmony_ci```cpp 4758c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name, 4768c339a94Sopenharmony_ci StaticGetterCallback getter, 4778c339a94Sopenharmony_ci StaticSetterCallback setter, 4788c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 4798c339a94Sopenharmony_ci void* data = nullptr); 4808c339a94Sopenharmony_ci``` 4818c339a94Sopenharmony_ci 4828c339a94Sopenharmony_ci- `[in] name`: Napi::Symbol that represents the name of a static accessor. 4838c339a94Sopenharmony_ci- `[in] getter`: The native function to call when a get access to the property 4848c339a94Sopenharmony_ciof a JavaScript class is performed. 4858c339a94Sopenharmony_ci- `[in] setter`: The native function to call when a set access to the property 4868c339a94Sopenharmony_ciof a JavaScript class is performed. 4878c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 4888c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 4898c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into getter or setter when 4908c339a94Sopenharmony_ciis invoked. 4918c339a94Sopenharmony_ci 4928c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents a static accessor 4938c339a94Sopenharmony_ciproperty of a JavaScript class. 4948c339a94Sopenharmony_ci 4958c339a94Sopenharmony_ci### StaticAccessor 4968c339a94Sopenharmony_ci 4978c339a94Sopenharmony_ciCreates property descriptor that represents a static accessor property of a 4988c339a94Sopenharmony_ciJavaScript class. 4998c339a94Sopenharmony_ci 5008c339a94Sopenharmony_ci```cpp 5018c339a94Sopenharmony_citemplate <StaticGetterCallback getter, StaticSetterCallback setter=nullptr> 5028c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor( 5038c339a94Sopenharmony_ci const char* utf8name, 5048c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 5058c339a94Sopenharmony_ci void* data = nullptr); 5068c339a94Sopenharmony_ci``` 5078c339a94Sopenharmony_ci 5088c339a94Sopenharmony_ci- `[in] getter`: The native function to call when a get access to the property 5098c339a94Sopenharmony_ciof a JavaScript class is performed. 5108c339a94Sopenharmony_ci- `[in] setter`: The native function to call when a set access to the property 5118c339a94Sopenharmony_ciof a JavaScript class is performed. 5128c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string that represents the name of a static 5138c339a94Sopenharmony_ciaccessor property for the class. 5148c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 5158c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 5168c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into getter or setter when 5178c339a94Sopenharmony_ciis invoked. 5188c339a94Sopenharmony_ci 5198c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents a static accessor 5208c339a94Sopenharmony_ciproperty of a JavaScript class. 5218c339a94Sopenharmony_ci 5228c339a94Sopenharmony_ci### StaticAccessor 5238c339a94Sopenharmony_ci 5248c339a94Sopenharmony_ciCreates property descriptor that represents a static accessor property of a 5258c339a94Sopenharmony_ciJavaScript class. 5268c339a94Sopenharmony_ci 5278c339a94Sopenharmony_ci```cpp 5288c339a94Sopenharmony_citemplate <StaticGetterCallback getter, StaticSetterCallback setter=nullptr> 5298c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name, 5308c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default, 5318c339a94Sopenharmony_ci void* data = nullptr); 5328c339a94Sopenharmony_ci``` 5338c339a94Sopenharmony_ci 5348c339a94Sopenharmony_ci- `[in] getter`: The native function to call when a get access to the property 5358c339a94Sopenharmony_ciof a JavaScript class is performed. 5368c339a94Sopenharmony_ci- `[in] setter`: The native function to call when a set access to the property 5378c339a94Sopenharmony_ciof a JavaScript class is performed. 5388c339a94Sopenharmony_ci- `[in] name`: Napi::Symbol that represents the name of a static accessor. 5398c339a94Sopenharmony_ci- `[in] attributes`: The attributes associated with a particular property. 5408c339a94Sopenharmony_ciOne or more of `napi_property_attributes`. 5418c339a94Sopenharmony_ci- `[in] data`: User-provided data passed into getter or setter when 5428c339a94Sopenharmony_ciis invoked. 5438c339a94Sopenharmony_ci 5448c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents a static accessor 5458c339a94Sopenharmony_ciproperty of a JavaScript class. 5468c339a94Sopenharmony_ci 5478c339a94Sopenharmony_ci### StaticValue 5488c339a94Sopenharmony_ci 5498c339a94Sopenharmony_ciCreates property descriptor that represents an static value property of a 5508c339a94Sopenharmony_ciJavaScript class. 5518c339a94Sopenharmony_ci```cpp 5528c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue( 5538c339a94Sopenharmony_ci const char* utf8name, 5548c339a94Sopenharmony_ci Napi::Value value, 5558c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default); 5568c339a94Sopenharmony_ci``` 5578c339a94Sopenharmony_ci 5588c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string that represents the name of the static 5598c339a94Sopenharmony_ciproperty. 5608c339a94Sopenharmony_ci- `[in] value`: The value that's retrieved by a get access of the property. 5618c339a94Sopenharmony_ci- `[in] attributes`: The attributes to be associated with the property in 5628c339a94Sopenharmony_ciaddition to the napi_static attribute. One or more of 5638c339a94Sopenharmony_ci`napi_property_attributes`. 5648c339a94Sopenharmony_ci 5658c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents an static value 5668c339a94Sopenharmony_ciproperty of a JavaScript class 5678c339a94Sopenharmony_ci 5688c339a94Sopenharmony_ci### StaticValue 5698c339a94Sopenharmony_ci 5708c339a94Sopenharmony_ciCreates property descriptor that represents an static value property of a 5718c339a94Sopenharmony_ciJavaScript class. 5728c339a94Sopenharmony_ci```cpp 5738c339a94Sopenharmony_cistatic Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue(Symbol name, 5748c339a94Sopenharmony_ci Napi::Value value, 5758c339a94Sopenharmony_ci napi_property_attributes attributes = napi_default); 5768c339a94Sopenharmony_ci``` 5778c339a94Sopenharmony_ci 5788c339a94Sopenharmony_ci- `[in] name`: The `Napi::Symbol` object whose value is used to identify the 5798c339a94Sopenharmony_ciname of the static property. 5808c339a94Sopenharmony_ci- `[in] value`: The value that's retrieved by a get access of the property. 5818c339a94Sopenharmony_ci- `[in] attributes`: The attributes to be associated with the property in 5828c339a94Sopenharmony_ciaddition to the napi_static attribute. One or more of 5838c339a94Sopenharmony_ci`napi_property_attributes`. 5848c339a94Sopenharmony_ci 5858c339a94Sopenharmony_ciReturns `Napi::PropertyDescriptor` object that represents an static value 5868c339a94Sopenharmony_ciproperty of a JavaScript class 5878c339a94Sopenharmony_ci 5888c339a94Sopenharmony_ci[`Napi::InstanceWrap<T>`]: ./instance_wrap.md 589