18c339a94Sopenharmony_ci# Reference (template) 28c339a94Sopenharmony_ci 38c339a94Sopenharmony_ciHolds a counted reference to a [`Napi::Value`](value.md) object; initially a weak reference unless otherwise specified, may be changed to/from a strong reference by adjusting the refcount. 48c339a94Sopenharmony_ci 58c339a94Sopenharmony_ciThe referenced `Napi::Value` is not immediately destroyed when the reference count is zero; it is merely then eligible for garbage-collection if there are no other references to the `Napi::Value`. 68c339a94Sopenharmony_ci 78c339a94Sopenharmony_ci`Napi::Reference` objects allocated in static space, such as a global static instance, must call the `SuppressDestruct` method to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. Avoid using this if at all possible. 88c339a94Sopenharmony_ci 98c339a94Sopenharmony_ciThe following classes inherit, either directly or indirectly, from `Napi::Reference`: 108c339a94Sopenharmony_ci 118c339a94Sopenharmony_ci* [`Napi::ObjectWrap`](object_wrap.md) 128c339a94Sopenharmony_ci* [`Napi::ObjectReference`](object_reference.md) 138c339a94Sopenharmony_ci* [`Napi::FunctionReference`](function_reference.md) 148c339a94Sopenharmony_ci 158c339a94Sopenharmony_ci## Methods 168c339a94Sopenharmony_ci 178c339a94Sopenharmony_ci### Factory Method 188c339a94Sopenharmony_ci 198c339a94Sopenharmony_ci```cpp 208c339a94Sopenharmony_cistatic Napi::Reference<T> Napi::Reference::New(const T& value, uint32_t initialRefcount = 0); 218c339a94Sopenharmony_ci``` 228c339a94Sopenharmony_ci 238c339a94Sopenharmony_ci* `[in] value`: The value which is to be referenced. 248c339a94Sopenharmony_ci 258c339a94Sopenharmony_ci* `[in] initialRefcount`: The initial reference count. 268c339a94Sopenharmony_ci 278c339a94Sopenharmony_ci### Empty Constructor 288c339a94Sopenharmony_ci 298c339a94Sopenharmony_ci```cpp 308c339a94Sopenharmony_ciNapi::Reference::Reference(); 318c339a94Sopenharmony_ci``` 328c339a94Sopenharmony_ci 338c339a94Sopenharmony_ciCreates a new _empty_ `Napi::Reference` instance. 348c339a94Sopenharmony_ci 358c339a94Sopenharmony_ci### Constructor 368c339a94Sopenharmony_ci 378c339a94Sopenharmony_ci```cpp 388c339a94Sopenharmony_ciNapi::Reference::Reference(napi_env env, napi_value value); 398c339a94Sopenharmony_ci``` 408c339a94Sopenharmony_ci 418c339a94Sopenharmony_ci* `[in] env`: The `napi_env` environment in which to construct the `Napi::Reference` object. 428c339a94Sopenharmony_ci 438c339a94Sopenharmony_ci* `[in] value`: The Node-API primitive value to be held by the `Napi::Reference`. 448c339a94Sopenharmony_ci 458c339a94Sopenharmony_ci### Env 468c339a94Sopenharmony_ci 478c339a94Sopenharmony_ci```cpp 488c339a94Sopenharmony_ciNapi::Env Napi::Reference::Env() const; 498c339a94Sopenharmony_ci``` 508c339a94Sopenharmony_ci 518c339a94Sopenharmony_ciReturns the `Napi::Env` value in which the `Napi::Reference` was instantiated. 528c339a94Sopenharmony_ci 538c339a94Sopenharmony_ci### IsEmpty 548c339a94Sopenharmony_ci 558c339a94Sopenharmony_ci```cpp 568c339a94Sopenharmony_cibool Napi::Reference::IsEmpty() const; 578c339a94Sopenharmony_ci``` 588c339a94Sopenharmony_ci 598c339a94Sopenharmony_ciDetermines whether the value held by the `Napi::Reference` is empty. 608c339a94Sopenharmony_ci 618c339a94Sopenharmony_ci### Value 628c339a94Sopenharmony_ci 638c339a94Sopenharmony_ci```cpp 648c339a94Sopenharmony_ciT Napi::Reference::Value() const; 658c339a94Sopenharmony_ci``` 668c339a94Sopenharmony_ci 678c339a94Sopenharmony_ciReturns the value held by the `Napi::Reference`. 688c339a94Sopenharmony_ci 698c339a94Sopenharmony_ci### Ref 708c339a94Sopenharmony_ci 718c339a94Sopenharmony_ci```cpp 728c339a94Sopenharmony_ciuint32_t Napi::Reference::Ref() const; 738c339a94Sopenharmony_ci``` 748c339a94Sopenharmony_ci 758c339a94Sopenharmony_ciIncrements the reference count for the `Napi::Reference` and returns the resulting reference count. Throws an error if the increment fails. 768c339a94Sopenharmony_ci 778c339a94Sopenharmony_ci### Unref 788c339a94Sopenharmony_ci 798c339a94Sopenharmony_ci```cpp 808c339a94Sopenharmony_ciuint32_t Napi::Reference::Unref() const; 818c339a94Sopenharmony_ci``` 828c339a94Sopenharmony_ci 838c339a94Sopenharmony_ciDecrements the reference count for the `Napi::Reference` and returns the resulting reference count. Throws an error if the decrement fails. 848c339a94Sopenharmony_ci 858c339a94Sopenharmony_ci### Reset (Empty) 868c339a94Sopenharmony_ci 878c339a94Sopenharmony_ci```cpp 888c339a94Sopenharmony_civoid Napi::Reference::Reset(); 898c339a94Sopenharmony_ci``` 908c339a94Sopenharmony_ci 918c339a94Sopenharmony_ciSets the value held by the `Napi::Reference` to be empty. 928c339a94Sopenharmony_ci 938c339a94Sopenharmony_ci### Reset 948c339a94Sopenharmony_ci 958c339a94Sopenharmony_ci```cpp 968c339a94Sopenharmony_civoid Napi::Reference::Reset(const T& value, uint32_t refcount = 0); 978c339a94Sopenharmony_ci``` 988c339a94Sopenharmony_ci 998c339a94Sopenharmony_ci* `[in] value`: The value which is to be referenced. 1008c339a94Sopenharmony_ci 1018c339a94Sopenharmony_ci* `[in] initialRefcount`: The initial reference count. 1028c339a94Sopenharmony_ci 1038c339a94Sopenharmony_ciSets the value held by the `Napi::Reference`. 1048c339a94Sopenharmony_ci 1058c339a94Sopenharmony_ci### SuppressDestruct 1068c339a94Sopenharmony_ci 1078c339a94Sopenharmony_ci```cpp 1088c339a94Sopenharmony_civoid Napi::Reference::SuppressDestruct(); 1098c339a94Sopenharmony_ci``` 1108c339a94Sopenharmony_ci 1118c339a94Sopenharmony_ciCall this method on a `Napi::Reference` that is declared as static data to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. 1128c339a94Sopenharmony_ci 1138c339a94Sopenharmony_ci Avoid using this if at all possible. If you do need to use static data, **MAKE SURE** to warn your users that your addon is **NOT** threadsafe. 114