Skip to content

Commit

Permalink
doc: update attributes used by n-api samples (#35220)
Browse files Browse the repository at this point in the history
Update n-api samples to create object properties matching to the JS defaults.

Using non configurable, non writable properties has its usecases but
the JS default for class methods is `configurable` and `writable`.
Js properties set by JS code `obj.prop = val` are `configurable`, `writable`
and `enumerable`.
  • Loading branch information
Flarna authored and addaleax committed Sep 22, 2020
1 parent e21d1cd commit 89636e3
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1642,8 +1642,16 @@ provided by the addon:
```c
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc =
{"hello", NULL, Method, NULL, NULL, NULL, napi_default, NULL};
napi_property_descriptor desc = {
"hello",
NULL,
Method,
NULL,
NULL,
NULL,
napi_writable | napi_enumerable | napi_configurable,
NULL
};
status = napi_define_properties(env, exports, 1, &desc);
if (status != napi_ok) return NULL;
return exports;
Expand All @@ -1670,7 +1678,7 @@ To define a class so that new instances can be created (often used with
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor properties[] = {
{ "value", NULL, NULL, GetValue, SetValue, NULL, napi_default, NULL },
{ "value", NULL, NULL, GetValue, SetValue, NULL, napi_writable | napi_configurable, NULL },
DECLARE_NAPI_METHOD("plusOne", PlusOne),
DECLARE_NAPI_METHOD("multiply", Multiply),
};
Expand Down Expand Up @@ -3563,8 +3571,8 @@ if (status != napi_ok) return status;

// Set the properties
napi_property_descriptor descriptors[] = {
{ "foo", NULL, NULL, NULL, NULL, fooValue, napi_default, NULL },
{ "bar", NULL, NULL, NULL, NULL, barValue, napi_default, NULL }
{ "foo", NULL, NULL, NULL, NULL, fooValue, napi_writable | napi_configurable, NULL },
{ "bar", NULL, NULL, NULL, NULL, barValue, napi_writable | napi_configurable, NULL }
}
status = napi_define_properties(env,
obj,
Expand Down

0 comments on commit 89636e3

Please sign in to comment.