Christian Fraß
[add] server:lib:node_modules
Christian Fraß commited 6e9c872 at 2021-03-08 23:52:07
# Object Wrap
Class `Napi::ObjectWrap<T>` inherits from class [`Napi::InstanceWrap<T>`][].
The `Napi::ObjectWrap<T>` class is used to bind the lifetime of C++ code to a
JavaScript object. Once bound, each time an instance of the JavaScript object
is created, an instance of the C++ class will also be created. When a method
is called on the JavaScript object which is defined as an InstanceMethod, the
corresponding C++ method on the wrapped C++ class will be invoked.
In order to create a wrapper it's necessary to extend the
`Napi::ObjectWrap<T>` class which contains all the plumbing to connect
JavaScript code with a C++ object. Classes extending `Napi::ObjectWrap` can be
instantiated from JavaScript using the **new** operator, and their methods can
be directly invoked from JavaScript. The **wrap** word refers to a way of
grouping methods and state of the class because it will be necessary write
custom code to bridge each of your C++ class methods.
## Example
```cpp
#include <napi.h>
class Example : public Napi::ObjectWrap<Example> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
Example(const Napi::CallbackInfo& info);
static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
private:
double _value;
Napi::Value GetValue(const Napi::CallbackInfo& info);
Napi::Value SetValue(const Napi::CallbackInfo& info);
};
Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
// This method is used to hook the accessor and method callbacks
Napi::Function func = DefineClass(env, "Example", {
InstanceMethod<&Example::GetValue>("GetValue"),
InstanceMethod<&Example::SetValue>("SetValue"),
StaticMethod<&Example::CreateNewItem>("CreateNewItem"),
});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
// Create a persistent reference to the class constructor. This will allow
// a function called on a class prototype and a function
// called on instance of a class to be distinguished from each other.
*constructor = Napi::Persistent(func);
exports.Set("Example", func);
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX