Skip to content

Commit

Permalink
doc: document common pattern for instanceof checks
Browse files Browse the repository at this point in the history
Backport-PR-URL: #19447
PR-URL: #16699
Fixes: #13824
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
mhdawson authored and MylesBorins committed Apr 16, 2018
1 parent 976f32d commit 35dc8ba
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions doc/api/n-api.md
Expand Up @@ -3022,6 +3022,29 @@ constructor and methods can be called from JavaScript.
callback, [`napi_unwrap`][] obtains the C++ instance that is the target of
the call.

For wrapped objects it may be difficult to distinguish between a function
called on a class prototype and a function called on an instance of a class.
A common pattern used to address this problem is to save a persistent
reference to the class constructor for later `instanceof` checks.

As an example:

```C
napi_value MyClass_constructor = nullptr;
status = napi_get_reference_value(env, MyClass::es_constructor, &MyClass_constructor);
assert(napi_ok == status);
bool is_instance = false;
status = napi_instanceof(env, es_this, MyClass_constructor, &is_instance);
assert(napi_ok == status);
if (is_instance) {
// napi_unwrap() ...
} else {
// otherwise...
}
```
The reference must be freed once it is no longer needed.
### *napi_define_class*
<!-- YAML
added: v8.0.0
Expand Down

0 comments on commit 35dc8ba

Please sign in to comment.