18c339a94Sopenharmony_ci# Object Reference
28c339a94Sopenharmony_ci
38c339a94Sopenharmony_ci`Napi::ObjectReference` is a subclass of [`Napi::Reference`](reference.md), and is equivalent to an instance of `Napi::Reference<Object>`. This means that a `Napi::ObjectReference` holds a [`Napi::Object`](object.md), and a count of the number of references to that Object. When the count is greater than 0, an ObjectReference is not eligible for garbage collection. This ensures that the Object being held as a value of the ObjectReference will remain accessible, even if the original Object no longer is. However, ObjectReference is unique from a Reference since properties can be set and get to the Object itself that can be accessed through the ObjectReference.
48c339a94Sopenharmony_ci
58c339a94Sopenharmony_ciFor more general information on references, please consult [`Napi::Reference`](reference.md).
68c339a94Sopenharmony_ci
78c339a94Sopenharmony_ci## Example
88c339a94Sopenharmony_ci```cpp
98c339a94Sopenharmony_ci#include <napi.h>
108c339a94Sopenharmony_ci
118c339a94Sopenharmony_ciusing namespace Napi;
128c339a94Sopenharmony_ci
138c339a94Sopenharmony_civoid Init(Env env) {
148c339a94Sopenharmony_ci
158c339a94Sopenharmony_ci    // Create an empty ObjectReference that has an initial reference count of 2.
168c339a94Sopenharmony_ci    ObjectReference obj_ref = Reference<Object>::New(Object::New(env), 2);
178c339a94Sopenharmony_ci
188c339a94Sopenharmony_ci    // Set a couple of different properties on the reference.
198c339a94Sopenharmony_ci    obj_ref.Set("hello", String::New(env, "world"));
208c339a94Sopenharmony_ci    obj_ref.Set(42, "The Answer to Life, the Universe, and Everything");
218c339a94Sopenharmony_ci
228c339a94Sopenharmony_ci    // Get the properties using the keys.
238c339a94Sopenharmony_ci    Value val1 = obj_ref.Get("hello");
248c339a94Sopenharmony_ci    Value val2 = obj_ref.Get(42);
258c339a94Sopenharmony_ci}
268c339a94Sopenharmony_ci```
278c339a94Sopenharmony_ci
288c339a94Sopenharmony_ci## Methods
298c339a94Sopenharmony_ci
308c339a94Sopenharmony_ci### Initialization
318c339a94Sopenharmony_ci
328c339a94Sopenharmony_ci```cpp
338c339a94Sopenharmony_cistatic Napi::ObjectReference Napi::ObjectReference::New(const Napi::Object& value, uint32_t initialRefcount = 0);
348c339a94Sopenharmony_ci```
358c339a94Sopenharmony_ci
368c339a94Sopenharmony_ci* `[in] value`: The `Napi::Object` which is to be referenced.
378c339a94Sopenharmony_ci
388c339a94Sopenharmony_ci* `[in] initialRefcount`: The initial reference count.
398c339a94Sopenharmony_ci
408c339a94Sopenharmony_ciReturns the newly created reference.
418c339a94Sopenharmony_ci
428c339a94Sopenharmony_ci```cpp
438c339a94Sopenharmony_cistatic Napi::ObjectReference Napi::Weak(const Napi::Object& value);
448c339a94Sopenharmony_ci```
458c339a94Sopenharmony_ci
468c339a94Sopenharmony_ciCreates a "weak" reference to the value, in that the initial count of number of references is set to 0.
478c339a94Sopenharmony_ci
488c339a94Sopenharmony_ci* `[in] value`: The value which is to be referenced.
498c339a94Sopenharmony_ci
508c339a94Sopenharmony_ciReturns the newly created reference.
518c339a94Sopenharmony_ci
528c339a94Sopenharmony_ci```cpp
538c339a94Sopenharmony_cistatic Napi::ObjectReference Napi::Persistent(const Napi::Object& value);
548c339a94Sopenharmony_ci```
558c339a94Sopenharmony_ci
568c339a94Sopenharmony_ciCreates a "persistent" reference to the value, in that the initial count of number of references is set to 1.
578c339a94Sopenharmony_ci
588c339a94Sopenharmony_ci* `[in] value`: The value which is to be referenced.
598c339a94Sopenharmony_ci
608c339a94Sopenharmony_ciReturns the newly created reference.
618c339a94Sopenharmony_ci
628c339a94Sopenharmony_ci### Empty Constructor
638c339a94Sopenharmony_ci
648c339a94Sopenharmony_ci```cpp
658c339a94Sopenharmony_ciNapi::ObjectReference::ObjectReference();
668c339a94Sopenharmony_ci```
678c339a94Sopenharmony_ci
688c339a94Sopenharmony_ciReturns a new _empty_ `Napi::ObjectReference` instance.
698c339a94Sopenharmony_ci
708c339a94Sopenharmony_ci### Constructor
718c339a94Sopenharmony_ci
728c339a94Sopenharmony_ci```cpp
738c339a94Sopenharmony_ciNapi::ObjectReference::ObjectReference(napi_env env, napi_value value);
748c339a94Sopenharmony_ci```
758c339a94Sopenharmony_ci
768c339a94Sopenharmony_ci* `[in] env`: The `napi_env` environment in which to construct the `Napi::ObjectReference` object.
778c339a94Sopenharmony_ci
788c339a94Sopenharmony_ci* `[in] value`: The Node-API primitive value to be held by the `Napi::ObjectReference`.
798c339a94Sopenharmony_ci
808c339a94Sopenharmony_ciReturns the newly created reference.
818c339a94Sopenharmony_ci
828c339a94Sopenharmony_ci### Set
838c339a94Sopenharmony_ci```cpp
848c339a94Sopenharmony_cibool Napi::ObjectReference::Set(___ key, ___ value);
858c339a94Sopenharmony_ci```
868c339a94Sopenharmony_ci
878c339a94Sopenharmony_ci* `[in] key`: The name for the property being assigned.
888c339a94Sopenharmony_ci
898c339a94Sopenharmony_ci* `[in] value`: The value being assigned to the property.
908c339a94Sopenharmony_ci
918c339a94Sopenharmony_ciThe `key` can be any of the following types:
928c339a94Sopenharmony_ci- `const char*`
938c339a94Sopenharmony_ci- `const std::string`
948c339a94Sopenharmony_ci- `uint32_t`
958c339a94Sopenharmony_ci
968c339a94Sopenharmony_ciThe `value` can be any of the following types:
978c339a94Sopenharmony_ci- `napi_value`
988c339a94Sopenharmony_ci- `Napi::Value`
998c339a94Sopenharmony_ci- `const char*`
1008c339a94Sopenharmony_ci- `bool`
1018c339a94Sopenharmony_ci- `double`
1028c339a94Sopenharmony_ci
1038c339a94Sopenharmony_ci### Get
1048c339a94Sopenharmony_ci
1058c339a94Sopenharmony_ci```cpp
1068c339a94Sopenharmony_ciNapi::Value Napi::ObjectReference::Get(___ key) const;
1078c339a94Sopenharmony_ci```
1088c339a94Sopenharmony_ci
1098c339a94Sopenharmony_ci* `[in] key`: The name of the property to return the value for.
1108c339a94Sopenharmony_ci
1118c339a94Sopenharmony_ciReturns the [`Napi::Value`](value.md) associated with the key property. Returns NULL if no such key exists.
1128c339a94Sopenharmony_ci
1138c339a94Sopenharmony_ciThe `key` can be any of the following types:
1148c339a94Sopenharmony_ci- `const char*`
1158c339a94Sopenharmony_ci- `const std::string`
1168c339a94Sopenharmony_ci- `uint32_t`
1178c339a94Sopenharmony_ci
118