| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7fe6a8f commit 8dc05e4
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3022,6 +3022,29 @@ constructor and methods can be called from JavaScript. | |||
| 3022 | 3022 | callback, [`napi_unwrap`][] obtains the C++ instance that is the target of | |
| 3023 | 3023 | the call. | |
| 3024 | 3024 | ||
| 3025 | + For wrapped objects it may be difficult to distinguish between a function | ||
| 3026 | + called on a class prototype and a function called on an instance of a class. | ||
| 3027 | + A common pattern used to address this problem is to save a persistent | ||
| 3028 | + reference to the class constructor for later `instanceof` checks. | ||
| 3029 | + | ||
| 3030 | + As an example: | ||
| 3031 | + | ||
| 3032 | + ```C | ||
| 3033 | + napi_value MyClass_constructor = nullptr; | ||
| 3034 | + status = napi_get_reference_value(env, MyClass::es_constructor, &MyClass_constructor); | ||
| 3035 | + assert(napi_ok == status); | ||
| 3036 | + bool is_instance = false; | ||
| 3037 | + status = napi_instanceof(env, es_this, MyClass_constructor, &is_instance); | ||
| 3038 | + assert(napi_ok == status); | ||
| 3039 | + if (is_instance) { | ||
| 3040 | + // napi_unwrap() ... | ||
| 3041 | + } else { | ||
| 3042 | + // otherwise... | ||
| 3043 | + } | ||
| 3044 | + ``` | ||
| 3045 | + | ||
| 3046 | + The reference must be freed once it is no longer needed. | ||
| 3047 | + | ||
| 3025 | 3048 | ### *napi_define_class* | |
| 3026 | 3049 | <!-- YAML | |
| 3027 | 3050 | added: v8.0.0 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments