18c339a94Sopenharmony_ci# FunctionReference 28c339a94Sopenharmony_ci 38c339a94Sopenharmony_ci`Napi::FunctionReference` is a subclass of [`Napi::Reference`](reference.md), and 48c339a94Sopenharmony_ciis equivalent to an instance of `Napi::Reference<Napi::Function>`. This means 58c339a94Sopenharmony_cithat a `Napi::FunctionReference` holds a [`Napi::Function`](function.md), and a 68c339a94Sopenharmony_cicount of the number of references to that `Napi::Function`. When the count is 78c339a94Sopenharmony_cigreater than 0, a `Napi::FunctionReference` is not eligible for garbage collection. 88c339a94Sopenharmony_ciThis ensures that the `Function` will remain accessible, even if the original 98c339a94Sopenharmony_cireference to it is no longer available. 108c339a94Sopenharmony_ci`Napi::FunctionReference` allows the referenced JavaScript function object to be 118c339a94Sopenharmony_cicalled from a native add-on with two different methods: `Call` and `MakeCallback`. 128c339a94Sopenharmony_ciSee the documentation for [`Napi::Function`](function.md) for when `Call` should 138c339a94Sopenharmony_cibe used instead of `MakeCallback` and vice-versa. 148c339a94Sopenharmony_ci 158c339a94Sopenharmony_ciThe `Napi::FunctionReference` class inherits its behavior from the `Napi::Reference` 168c339a94Sopenharmony_ciclass (for more info see: [`Napi::Reference`](reference.md)). 178c339a94Sopenharmony_ci 188c339a94Sopenharmony_ci## Methods 198c339a94Sopenharmony_ci 208c339a94Sopenharmony_ci### Weak 218c339a94Sopenharmony_ci 228c339a94Sopenharmony_ciCreates a "weak" reference to the value, in that the initial reference count is 238c339a94Sopenharmony_ciset to 0. 248c339a94Sopenharmony_ci 258c339a94Sopenharmony_ci```cpp 268c339a94Sopenharmony_cistatic Napi::FunctionReference Napi::Weak(const Napi::Function& value); 278c339a94Sopenharmony_ci``` 288c339a94Sopenharmony_ci 298c339a94Sopenharmony_ci- `[in] value`: The value which is to be referenced. 308c339a94Sopenharmony_ci 318c339a94Sopenharmony_ciReturns the newly created reference. 328c339a94Sopenharmony_ci 338c339a94Sopenharmony_ci### Persistent 348c339a94Sopenharmony_ci 358c339a94Sopenharmony_ciCreates a "persistent" reference to the value, in that the initial reference 368c339a94Sopenharmony_cicount is set to 1. 378c339a94Sopenharmony_ci 388c339a94Sopenharmony_ci```cpp 398c339a94Sopenharmony_cistatic Napi::FunctionReference Napi::Persistent(const Napi::Function& value); 408c339a94Sopenharmony_ci``` 418c339a94Sopenharmony_ci 428c339a94Sopenharmony_ci- `[in] value`: The value which is to be referenced. 438c339a94Sopenharmony_ci 448c339a94Sopenharmony_ciReturns the newly created reference. 458c339a94Sopenharmony_ci 468c339a94Sopenharmony_ci### Constructor 478c339a94Sopenharmony_ci 488c339a94Sopenharmony_ciCreates a new empty instance of `Napi::FunctionReference`. 498c339a94Sopenharmony_ci 508c339a94Sopenharmony_ci```cpp 518c339a94Sopenharmony_ciNapi::FunctionReference::FunctionReference(); 528c339a94Sopenharmony_ci``` 538c339a94Sopenharmony_ci 548c339a94Sopenharmony_ci### Constructor 558c339a94Sopenharmony_ci 568c339a94Sopenharmony_ciCreates a new instance of the `Napi::FunctionReference`. 578c339a94Sopenharmony_ci 588c339a94Sopenharmony_ci```cpp 598c339a94Sopenharmony_ciNapi::FunctionReference::FunctionReference(napi_env env, napi_ref ref); 608c339a94Sopenharmony_ci``` 618c339a94Sopenharmony_ci 628c339a94Sopenharmony_ci- `[in] env`: The environment in which to construct the `Napi::FunctionReference` object. 638c339a94Sopenharmony_ci- `[in] ref`: The Node-API reference to be held by the `Napi::FunctionReference`. 648c339a94Sopenharmony_ci 658c339a94Sopenharmony_ciReturns a newly created `Napi::FunctionReference` object. 668c339a94Sopenharmony_ci 678c339a94Sopenharmony_ci### New 688c339a94Sopenharmony_ci 698c339a94Sopenharmony_ciConstructs a new instance by calling the constructor held by this reference. 708c339a94Sopenharmony_ci 718c339a94Sopenharmony_ci```cpp 728c339a94Sopenharmony_ciNapi::Object Napi::FunctionReference::New(const std::initializer_list<napi_value>& args) const; 738c339a94Sopenharmony_ci``` 748c339a94Sopenharmony_ci 758c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value` representing 768c339a94Sopenharmony_cithe arguments of the constructor function. 778c339a94Sopenharmony_ci 788c339a94Sopenharmony_ciReturns a new JavaScript object. 798c339a94Sopenharmony_ci 808c339a94Sopenharmony_ci### New 818c339a94Sopenharmony_ci 828c339a94Sopenharmony_ciConstructs a new instance by calling the constructor held by this reference. 838c339a94Sopenharmony_ci 848c339a94Sopenharmony_ci```cpp 858c339a94Sopenharmony_ciNapi::Object Napi::FunctionReference::New(const std::vector<napi_value>& args) const; 868c339a94Sopenharmony_ci``` 878c339a94Sopenharmony_ci 888c339a94Sopenharmony_ci- `[in] args`: Vector of JavaScript values as `napi_value` representing the 898c339a94Sopenharmony_ciarguments of the constructor function. 908c339a94Sopenharmony_ci 918c339a94Sopenharmony_ciReturns a new JavaScript object. 928c339a94Sopenharmony_ci 938c339a94Sopenharmony_ci### Call 948c339a94Sopenharmony_ci 958c339a94Sopenharmony_ciCalls a referenced Javascript function from a native add-on. 968c339a94Sopenharmony_ci 978c339a94Sopenharmony_ci```cpp 988c339a94Sopenharmony_ciNapi::Value Napi::FunctionReference::Call(const std::initializer_list<napi_value>& args) const; 998c339a94Sopenharmony_ci``` 1008c339a94Sopenharmony_ci 1018c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value` representing 1028c339a94Sopenharmony_cithe arguments of the referenced function. 1038c339a94Sopenharmony_ci 1048c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript object returned by the referenced 1058c339a94Sopenharmony_cifunction. 1068c339a94Sopenharmony_ci 1078c339a94Sopenharmony_ci### Call 1088c339a94Sopenharmony_ci 1098c339a94Sopenharmony_ciCalls a referenced JavaScript function from a native add-on. 1108c339a94Sopenharmony_ci 1118c339a94Sopenharmony_ci```cpp 1128c339a94Sopenharmony_ciNapi::Value Napi::FunctionReference::Call(const std::vector<napi_value>& args) const; 1138c339a94Sopenharmony_ci``` 1148c339a94Sopenharmony_ci 1158c339a94Sopenharmony_ci- `[in] args`: Vector of JavaScript values as `napi_value` representing the 1168c339a94Sopenharmony_ciarguments of the referenced function. 1178c339a94Sopenharmony_ci 1188c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript object returned by the referenced 1198c339a94Sopenharmony_cifunction. 1208c339a94Sopenharmony_ci 1218c339a94Sopenharmony_ci### Call 1228c339a94Sopenharmony_ci 1238c339a94Sopenharmony_ciCalls a referenced JavaScript function from a native add-on. 1248c339a94Sopenharmony_ci 1258c339a94Sopenharmony_ci```cpp 1268c339a94Sopenharmony_ciNapi::Value Napi::FunctionReference::Call(napi_value recv, const std::initializer_list<napi_value>& args) const; 1278c339a94Sopenharmony_ci``` 1288c339a94Sopenharmony_ci 1298c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the referenced function when it's called. 1308c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value` representing 1318c339a94Sopenharmony_cithe arguments of the referenced function. 1328c339a94Sopenharmony_ci 1338c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript object returned by the referenced 1348c339a94Sopenharmony_cifunction. 1358c339a94Sopenharmony_ci 1368c339a94Sopenharmony_ci### Call 1378c339a94Sopenharmony_ci 1388c339a94Sopenharmony_ciCalls a referenced JavaScript function from a native add-on. 1398c339a94Sopenharmony_ci 1408c339a94Sopenharmony_ci```cpp 1418c339a94Sopenharmony_ciNapi::Value Napi::FunctionReference::Call(napi_value recv, const std::vector<napi_value>& args) const; 1428c339a94Sopenharmony_ci``` 1438c339a94Sopenharmony_ci 1448c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the referenced function when it's called. 1458c339a94Sopenharmony_ci- `[in] args`: Vector of JavaScript values as `napi_value` representing the 1468c339a94Sopenharmony_ciarguments of the referenced function. 1478c339a94Sopenharmony_ci 1488c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript object returned by the referenced 1498c339a94Sopenharmony_cifunction. 1508c339a94Sopenharmony_ci 1518c339a94Sopenharmony_ci### Call 1528c339a94Sopenharmony_ci 1538c339a94Sopenharmony_ciCalls a referenced JavaScript function from a native add-on. 1548c339a94Sopenharmony_ci 1558c339a94Sopenharmony_ci```cpp 1568c339a94Sopenharmony_ciNapi::Value Napi::FunctionReference::Call(napi_value recv, size_t argc, const napi_value* args) const; 1578c339a94Sopenharmony_ci``` 1588c339a94Sopenharmony_ci 1598c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the referenced function when it's called. 1608c339a94Sopenharmony_ci- `[in] argc`: The number of arguments passed to the referenced function. 1618c339a94Sopenharmony_ci- `[in] args`: Array of JavaScript values as `napi_value` representing the 1628c339a94Sopenharmony_ciarguments of the referenced function. 1638c339a94Sopenharmony_ci 1648c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript object returned by the referenced 1658c339a94Sopenharmony_cifunction. 1668c339a94Sopenharmony_ci 1678c339a94Sopenharmony_ci 1688c339a94Sopenharmony_ci### MakeCallback 1698c339a94Sopenharmony_ci 1708c339a94Sopenharmony_ciCalls a referenced JavaScript function from a native add-on after an asynchronous 1718c339a94Sopenharmony_cioperation. 1728c339a94Sopenharmony_ci 1738c339a94Sopenharmony_ci```cpp 1748c339a94Sopenharmony_ciNapi::Value Napi::FunctionReference::MakeCallback(napi_value recv, const std::initializer_list<napi_value>& args, napi_async_context = nullptr) const; 1758c339a94Sopenharmony_ci``` 1768c339a94Sopenharmony_ci 1778c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the referenced function when it's called. 1788c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value` representing 1798c339a94Sopenharmony_cithe arguments of the referenced function. 1808c339a94Sopenharmony_ci- `[in] context`: Context for the async operation that is invoking the callback. 1818c339a94Sopenharmony_ciThis should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). 1828c339a94Sopenharmony_ciHowever `nullptr` is also allowed, which indicates the current async context 1838c339a94Sopenharmony_ci(if any) is to be used for the callback. 1848c339a94Sopenharmony_ci 1858c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript object returned by the referenced 1868c339a94Sopenharmony_cifunction. 1878c339a94Sopenharmony_ci 1888c339a94Sopenharmony_ci### MakeCallback 1898c339a94Sopenharmony_ci 1908c339a94Sopenharmony_ciCalls a referenced JavaScript function from a native add-on after an asynchronous 1918c339a94Sopenharmony_cioperation. 1928c339a94Sopenharmony_ci 1938c339a94Sopenharmony_ci```cpp 1948c339a94Sopenharmony_ciNapi::Value Napi::FunctionReference::MakeCallback(napi_value recv, const std::vector<napi_value>& args, napi_async_context context = nullptr) const; 1958c339a94Sopenharmony_ci``` 1968c339a94Sopenharmony_ci 1978c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the referenced function when it's called. 1988c339a94Sopenharmony_ci- `[in] args`: Vector of JavaScript values as `napi_value` representing the 1998c339a94Sopenharmony_ciarguments of the referenced function. 2008c339a94Sopenharmony_ci- `[in] context`: Context for the async operation that is invoking the callback. 2018c339a94Sopenharmony_ciThis should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). 2028c339a94Sopenharmony_ciHowever `nullptr` is also allowed, which indicates the current async context 2038c339a94Sopenharmony_ci(if any) is to be used for the callback. 2048c339a94Sopenharmony_ci 2058c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript object returned by the referenced 2068c339a94Sopenharmony_cifunction. 2078c339a94Sopenharmony_ci 2088c339a94Sopenharmony_ci### MakeCallback 2098c339a94Sopenharmony_ci 2108c339a94Sopenharmony_ciCalls a referenced JavaScript function from a native add-on after an asynchronous 2118c339a94Sopenharmony_cioperation. 2128c339a94Sopenharmony_ci 2138c339a94Sopenharmony_ci```cpp 2148c339a94Sopenharmony_ciNapi::Value Napi::FunctionReference::MakeCallback(napi_value recv, size_t argc, const napi_value* args, napi_async_context context = nullptr) const; 2158c339a94Sopenharmony_ci``` 2168c339a94Sopenharmony_ci 2178c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the referenced function when it's called. 2188c339a94Sopenharmony_ci- `[in] argc`: The number of arguments passed to the referenced function. 2198c339a94Sopenharmony_ci- `[in] args`: Array of JavaScript values as `napi_value` representing the 2208c339a94Sopenharmony_ciarguments of the referenced function. 2218c339a94Sopenharmony_ci- `[in] context`: Context for the async operation that is invoking the callback. 2228c339a94Sopenharmony_ciThis should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). 2238c339a94Sopenharmony_ciHowever `nullptr` is also allowed, which indicates the current async context 2248c339a94Sopenharmony_ci(if any) is to be used for the callback. 2258c339a94Sopenharmony_ci 2268c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript object returned by the referenced 2278c339a94Sopenharmony_cifunction. 2288c339a94Sopenharmony_ci 2298c339a94Sopenharmony_ci## Operator 2308c339a94Sopenharmony_ci 2318c339a94Sopenharmony_ci```cpp 2328c339a94Sopenharmony_ciNapi::Value operator ()(const std::initializer_list<napi_value>& args) const; 2338c339a94Sopenharmony_ci``` 2348c339a94Sopenharmony_ci 2358c339a94Sopenharmony_ci- `[in] args`: Initializer list of reference to JavaScript values as `napi_value` 2368c339a94Sopenharmony_ci 2378c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the referenced 2388c339a94Sopenharmony_cifunction. 239