Skip to content

Commit 89636e3

Browse files
Flarnaaddaleax
authored andcommitted
doc: update attributes used by n-api samples (#35220)
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`.
1 parent e21d1cd commit 89636e3

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

doc/api/n-api.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -1642,8 +1642,16 @@ provided by the addon:
16421642
```c
16431643
napi_value Init(napi_env env, napi_value exports) {
16441644
napi_status status;
1645-
napi_property_descriptor desc =
1646-
{"hello", NULL, Method, NULL, NULL, NULL, napi_default, NULL};
1645+
napi_property_descriptor desc = {
1646+
"hello",
1647+
NULL,
1648+
Method,
1649+
NULL,
1650+
NULL,
1651+
NULL,
1652+
napi_writable | napi_enumerable | napi_configurable,
1653+
NULL
1654+
};
16471655
status = napi_define_properties(env, exports, 1, &desc);
16481656
if (status != napi_ok) return NULL;
16491657
return exports;
@@ -1670,7 +1678,7 @@ To define a class so that new instances can be created (often used with
16701678
napi_value Init(napi_env env, napi_value exports) {
16711679
napi_status status;
16721680
napi_property_descriptor properties[] = {
1673-
{ "value", NULL, NULL, GetValue, SetValue, NULL, napi_default, NULL },
1681+
{ "value", NULL, NULL, GetValue, SetValue, NULL, napi_writable | napi_configurable, NULL },
16741682
DECLARE_NAPI_METHOD("plusOne", PlusOne),
16751683
DECLARE_NAPI_METHOD("multiply", Multiply),
16761684
};
@@ -3563,8 +3571,8 @@ if (status != napi_ok) return status;
35633571

35643572
// Set the properties
35653573
napi_property_descriptor descriptors[] = {
3566-
{ "foo", NULL, NULL, NULL, NULL, fooValue, napi_default, NULL },
3567-
{ "bar", NULL, NULL, NULL, NULL, barValue, napi_default, NULL }
3574+
{ "foo", NULL, NULL, NULL, NULL, fooValue, napi_writable | napi_configurable, NULL },
3575+
{ "bar", NULL, NULL, NULL, NULL, barValue, napi_writable | napi_configurable, NULL }
35683576
}
35693577
status = napi_define_properties(env,
35703578
obj,

0 commit comments

Comments
 (0)