1# Working with Property Using Node-API 2 3## Introduction 4 5Node-API provides APIs for obtaining and setting properties of ArkTS objects in C/C++. Properly using these APIs help to implement more complex functionalities and logic. 6 7## Basic Concepts 8 9Before working with ArkTS objects using Node-API, you need to understand the following concepts: 10 11- Object: a composite data type that allows values of different types in an independent entity in ArkTS. An object is a collection of properties and methods. A property is a value associated with the object, and a method is an operation that the object can perform. 12- Property: a feature, in the key-value format, of an object in ArkTS. Each property has a name (key or identifier) and a value. The property value can be of any data type, including the basic type, object, and function. 13- Enumerable property: a property in ArkTS with **enumerable** set to **true**. An enumerable property can be traversed by **for...in**. 14- Own property: a property defined for an object rather than inherited from the prototype chain. 15 16## Available APIs 17 18The following table lists the APIs for manipulating ArkTS object properties. 19| API| Description| 20| -------- | -------- | 21| napi_get_property_names | Obtains the names of the enumerable properties of an object in an array of strings. | 22| napi_set_property | Adds a property to an object or modifies a property value of an object.| 23| napi_get_property | Obtains the requested property of an object. You can use this API to obtain the property value of an ArkTS object and pass it to another function for processing.| 24| napi_has_property | Checks whether an object has the specified property. Before a property is accessed, you can call this API to check whether the object has this property. This can prevent the exception or error caused due to the absence of the property.| 25| napi_delete_property | Deletes a property from an ArkTS object.| 26| napi_has_own_property | Checks whether an object has the specified own property.| 27| napi_set_named_property | Sets a property with the specified name for an ArkTS object.| 28| napi_get_named_property | Obtains the value of a property in an ArkTS object.| 29| napi_has_named_property | Checks whether an ArkTS object has the property with the specified name.| 30| napi_define_properties | Defines multiple properties for an ArkTS object.| 31| napi_get_all_property_names | Obtains the names of all properties of an ArkTS object.| 32 33## Example 34 35If you are just starting out with Node-API, see [Node-API Development Process](use-napi-process.md). The following demonstrates only the C++ and ArkTS code related to property management. 36 37### napi_get_property_names 38 39Use **napi_get_property_names** to obtain the names of the enumerable properties of an object in an array of strings. 40 41CPP code: 42 43```cpp 44#include "napi/native_api.h" 45 46static napi_value GetPropertyNames(napi_env env, napi_callback_info info) 47{ 48 // Parse the ArkTS input parameters. 49 size_t argc = 1; 50 napi_value args[1] = {nullptr}; 51 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 52 // Obtain the names of all the enumerable properties of the object in the form of a string array and output the string array in result. 53 napi_value result; 54 napi_status status = napi_get_property_names(env, args[0], &result); 55 if (status != napi_ok) { 56 napi_throw_error(env, nullptr, "Node-API napi_get_property_names fail"); 57 return nullptr; 58 } 59 return result; 60} 61``` 62 63API declaration: 64 65```ts 66// index.d.ts 67export const getPropertyNames: (obj: Object) => Array<string> | void; 68``` 69 70ArkTS code: 71 72```ts 73import hilog from '@ohos.hilog' 74import testNapi from 'libentry.so' 75try { 76 class Obj { 77 data: number = 0 78 message: string = "" 79 } 80 let obj: Obj = { data: 0, message: "hello world"}; 81 let propertyNames = testNapi.getPropertyNames(obj); 82 if (Array.isArray(propertyNames) && propertyNames.length > 0) { 83 hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_property_names: %{public}s', propertyNames[0]); 84 hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_property_names: %{public}s', propertyNames[1]); 85 } 86} catch (error) { 87 hilog.error(0x0000, 'testTag', 'Test Node-API napi_get_property_names error: %{public}s', error.message); 88} 89``` 90 91### napi_set_property 92 93Use **napi_set_property** to set a property for an object. 94 95CPP code: 96 97```cpp 98#include "napi/native_api.h" 99 100static napi_value SetProperty(napi_env env, napi_callback_info info) 101{ 102 // Obtain the parameters passed from ArkTS. The first parameter specifies the object, the second parameter specifies the property name, and the third parameter specifies the property value to set. 103 size_t argc = 3; 104 napi_value args[3] = {nullptr}; 105 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 106 if (status != napi_ok) { 107 napi_throw_error(env, nullptr, "Node-API napi_get_cb_info fail"); 108 } 109 // Call napi_set_property to set the property name and value to the object. If the operation fails, throw an error. 110 status = napi_set_property(env, args[0], args[1], args[2]); 111 if (status != napi_ok) { 112 napi_throw_error(env, nullptr, "Node-API napi_set_property fail"); 113 return nullptr; 114 } 115 // Return the object that is successfully set. 116 return args[0]; 117} 118``` 119 120API declaration: 121 122```ts 123// index.d.ts 124export const setProperty: (obj: Object, key: String, value: string) => Object | void; 125``` 126 127ArkTS code: 128 129```ts 130import hilog from '@ohos.hilog' 131import testNapi from 'libentry.so' 132try { 133 class Obj { 134 data: number = 0 135 message: string = "" 136 } 137 let obj: Obj = { data: 0, message: "hello world"}; 138 let result = testNapi.setProperty(obj, "code", "hi"); 139 hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_property: %{public}s', JSON.stringify(result)); 140} catch (error) { 141 hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_property error: %{public}s', error.message); 142} 143``` 144 145### napi_get_property 146 147Use **napi_get_property** to obtain the value of the specified property in an object. 148 149CPP code: 150 151```cpp 152#include "napi/native_api.h" 153 154static napi_value GetProperty(napi_env env, napi_callback_info info) 155{ 156 // Obtain the two parameters passed from ArkTS. 157 size_t argc = 2; 158 napi_value args[2] = {nullptr}; 159 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 160 // The first parameter specifies the target object, and the second specifies the property name. Call napi_get_property to obtain the value of the property. 161 napi_value result; 162 napi_status status = napi_get_property(env, args[0], args[1], &result); 163 if (status != napi_ok) { 164 napi_throw_error(env, nullptr, "Node-API napi_get_property fail"); 165 return nullptr; 166 } 167 return result; 168} 169``` 170 171API declaration: 172 173```ts 174// index.d.ts 175export const getProperty: (obj: Object, key: string) => string | void; 176``` 177 178ArkTS code: 179 180```ts 181import hilog from '@ohos.hilog' 182import testNapi from 'libentry.so' 183try { 184 class Obj { 185 data: number = 0 186 message: string = "" 187 } 188 let obj: Obj = { data: 0, message: "hello world"}; 189 hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_property: %{public}s', testNapi.getProperty(obj, "message")); 190} catch (error) { 191 hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_property error: %{public}s', error.message); 192} 193``` 194 195### napi_has_property 196 197Use **napi_has_property** to check whether an object has the specified property. This can prevent the exception or error caused by access to a property that does not exist. 198 199CPP code: 200 201```cpp 202#include "napi/native_api.h" 203 204static napi_value HasProperty(napi_env env, napi_callback_info info) 205{ 206 // Pass in two parameters from ArkTS. The first parameter specifies the target object, and the second parameter specifies the property to check. 207 size_t argc = 2; 208 napi_value args[2] = {nullptr}; 209 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 210 211 // Pass the parameters to napi_has_property. If the API is successfully called, convert the result to napi_value and return napi_value. Otherwise, throw an error. 212 bool result; 213 napi_status status = napi_has_property(env, args[0], args[1], &result); 214 if (status != napi_ok) { 215 napi_throw_error(env, nullptr, "Node-API napi_has_property fail"); 216 return nullptr; 217 } 218 219 // If the property exists in the object, output true, convert the result to napi_value, and return napi_value. 220 napi_value returnReslut; 221 napi_get_boolean(env, result, &returnReslut); 222 return returnReslut; 223} 224``` 225 226API declaration: 227 228```ts 229// index.d.ts 230export const hasProperty: (obj: Object, key: number | string) => boolean | void; 231``` 232 233ArkTS code: 234 235```ts 236import hilog from '@ohos.hilog' 237import testNapi from 'libentry.so' 238try { 239 class Obj { 240 data: number = 0 241 message: string = "" 242 } 243 let obj: Obj = { data: 0, message: "hello world"}; 244 let resultFalse = testNapi.hasProperty(obj, 0); 245 let resultTrue = testNapi.hasProperty(obj, "data"); 246 hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_property: %{public}s', JSON.stringify(resultFalse)); 247 hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_property: %{public}s', JSON.stringify(resultTrue)); 248} catch (error) { 249 hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_property error: %{public}s', error.message); 250} 251``` 252 253### napi_delete_property 254 255Use **napi_delete_property** to delete the property specified by **key** from an object. 256If the object is a non-extensible object or the property is not configurable, the property cannot be deleted. 257 258CPP code: 259 260```cpp 261#include "napi/native_api.h" 262 263// Delete the specified property from the object and return a bool value indicating whether the deletion is successful. 264static napi_value DeleteProperty(napi_env env, napi_callback_info info) 265{ 266 // Obtain the two parameters passed from ArkTS. 267 size_t argc = 2; 268 napi_value args[2] = {nullptr}; 269 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 270 271 napi_valuetype valueType; 272 napi_typeof(env, args[0], &valueType); 273 if (valueType != napi_object) { 274 napi_throw_error(env, nullptr, "Expects an object as argument."); 275 return nullptr; 276 } 277 // Delete the specified property and store the operation result in result. 278 bool result = false; 279 napi_status status = napi_delete_property(env, args[0], args[1], &result); 280 if (status != napi_ok) { 281 napi_throw_error(env, nullptr, "Node-API napi_delete_property failed"); 282 return nullptr; 283 } 284 // Convert the bool value to napi_value and return it. 285 napi_value ret; 286 napi_get_boolean(env, result, &ret); 287 return ret; 288} 289``` 290 291API declaration: 292 293```ts 294// index.d.ts 295export const deleteProperty: (obj: Object, key:string) => boolean; 296``` 297 298ArkTS code: 299 300```ts 301import hilog from '@ohos.hilog' 302import testNapi from 'libentry.so' 303class Obj { 304 first: number = 0; 305} 306let obj: Obj = { first: 1}; 307hilog.info(0x0000, 'testTag', 'Test Node-API napi_delete_property first: %{public}s', testNapi.deleteProperty(obj, 'first')); 308// Set the new property to unconfigurable. 309// The Object.defineProperty method is not supported in DevEco Studio 4.1.0.400 or later. It must be used in TS. 310Object.defineProperty(obj, 'config', { 311 configurable: false, 312 value: "value" 313}) 314hilog.info(0x0000, 'testTag', 'Test Node-API napi_delete_property config: %{public}s', testNapi.deleteProperty(obj, 'config')); 315``` 316 317### napi_has_own_property 318 319Use **napi_has_own_property** to check whether an ArkTS object has its own property. 320 321CPP code: 322 323```cpp 324#include "napi/native_api.h" 325 326static napi_value NapiHasOwnProperty(napi_env env, napi_callback_info info) 327{ 328 // Obtain the two parameters passed from ArkTS. 329 size_t argc = 2; 330 napi_value args[2] = {nullptr}; 331 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 332 // Check whether the first parameter is an object. 333 napi_valuetype valueTypeObj; 334 napi_typeof(env, args[0], &valueTypeObj); 335 if (valueTypeObj != napi_object) { 336 napi_throw_error(env, nullptr, "First argument must be an object."); 337 return nullptr; 338 } 339 // Check whether the second parameter is a string. 340 napi_valuetype valuetypeStr; 341 napi_typeof(env, args[1], &valuetypeStr); 342 if (valuetypeStr != napi_string) { 343 napi_throw_error(env, nullptr, "Second argument must be a string."); 344 return nullptr; 345 } 346 // Check whether the object has the specified property and return the result in hasProperty. 347 bool hasProperty; 348 napi_status status = napi_has_own_property(env, args[0], args[1], &hasProperty); 349 if (status != napi_ok) { 350 napi_throw_error(env, nullptr, "napi_has_own_property failed"); 351 return nullptr; 352 } 353 // Convert the bool value to napi_value and return it. 354 napi_value result; 355 napi_get_boolean(env, hasProperty, &result); 356 return result; 357} 358``` 359 360API declaration: 361 362```ts 363// index.d.ts 364export const napiHasOwnProperty: (obj: Object, key:string) => boolean | void; 365``` 366 367ArkTS code: 368 369```ts 370import hilog from '@ohos.hilog' 371import testNapi from 'libentry.so' 372 373let myObj = { 'myProperty': 1 }; 374let inheritedObj = { 'inheritedProperty': 2 }; 375// The Object.setPrototypeOf method is not supported in DevEco Studio 4.1.0.400 or later. It must be used in TS. 376Object.setPrototypeOf(myObj, inheritedObj); 377hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_own_property my: %{public}s', testNapi.napiHasOwnProperty(myObj, 'myProperty')); 378hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_own_property inherited: %{public}s', testNapi.napiHasOwnProperty(myObj, 'inheritedProperty')); 379``` 380 381### napi_set_named_property 382 383Use **napi_set_named_property** to set a property for an ArkTS object. 384 385CPP code: 386 387```cpp 388#include "napi/native_api.h" 389 390static napi_value NapiSetNamedProperty(napi_env env, napi_callback_info info) 391{ 392 // Obtain the parameter passed from ArkTS. 393 size_t argc = 1; 394 napi_value str; 395 const int32_t strLength = 32; 396 char strKey[strLength] = ""; 397 napi_get_cb_info(env, info, &argc, &str, nullptr, nullptr); 398 // Obtain the string passed in and store it in strKey. 399 size_t keyLength; 400 napi_get_value_string_utf8(env, str, strKey, strLength, &keyLength); 401 // Create an object. 402 napi_value newObj; 403 napi_create_object(env, &newObj); 404 // Set the property value to 1234. 405 int32_t value = 1234; 406 napi_value numValue; 407 napi_create_int32(env, value, &numValue); 408 // Associate the integer value with the property name. 409 napi_status status = napi_set_named_property(env, newObj, strKey, numValue); 410 if (status != napi_ok) { 411 napi_throw_error(env, nullptr, "napi_set_named_property failed"); 412 return nullptr; 413 } 414 // Return the newObj object with the specified property set. 415 return newObj; 416} 417``` 418 419API declaration: 420 421```ts 422// index.d.ts 423export const napiSetNamedProperty: (key: string) => Object | void; 424``` 425 426ArkTS code: 427 428```ts 429import hilog from '@ohos.hilog' 430import testNapi from 'libentry.so' 431 432let obj = testNapi.napiSetNamedProperty('myProperty'); 433let objAsString = JSON.stringify(obj); 434hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_named_property: %{public}s', objAsString); 435``` 436 437### napi_get_named_property 438 439Use **napi_get_named_property** to obtain the value of the specified property from an ArkTS object. 440 441CPP code: 442 443```cpp 444#include "napi/native_api.h" 445 446static napi_value NapiGetNamedProperty(napi_env env, napi_callback_info info) 447{ 448 // Obtain the two parameters passed from ArkTS. 449 size_t argc = 2; 450 napi_value args[2] = {nullptr}; 451 const int32_t strLength = 32; 452 char strKey[strLength] = ""; 453 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 454 // Obtain the name of the property to obtain. 455 size_t keyLength; 456 napi_get_value_string_utf8(env, args[1], strKey, strLength, &keyLength); 457 // Obtain the value of the property and store it in result. 458 napi_value result; 459 napi_status status = napi_get_named_property(env, args[0], strKey, &result); 460 if (status != napi_ok) { 461 napi_throw_error(env, nullptr, "napi_get_named_property failed"); 462 return nullptr; 463 } 464 // Return result. 465 return result; 466} 467``` 468 469API declaration: 470 471```ts 472// index.d.ts 473export const napiGetNamedProperty: (obj: Object, key:string) => boolean | number | string | Object | void; 474``` 475 476ArkTS code: 477 478```ts 479import hilog from '@ohos.hilog' 480import testNapi from 'libentry.so' 481 482interface NestedObj { 483 nestedStr: string; 484 nestedNum: number; 485} 486class Obj { 487 str: string = ""; 488 num: number = 0; 489 bol: boolean = false; 490 nestedObj: NestedObj = { nestedStr: "", nestedNum: 0 }; 491} 492let obj: Obj = {str: "bar", num: 42, bol: true, 493 nestedObj: { nestedStr: "nestedValue", nestedNum: 123 }}; 494hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}s', testNapi.napiGetNamedProperty(obj, 'str')); 495hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}d', testNapi.napiGetNamedProperty(obj, 'num')); 496hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}s', testNapi.napiGetNamedProperty(obj, 'bol')); 497let nestedObj = testNapi.napiGetNamedProperty(obj, 'nestedObj'); 498let objAsString = JSON.stringify(nestedObj); 499hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}s', objAsString); 500hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}s', testNapi.napiGetNamedProperty(obj, 'null')); 501``` 502 503### napi_has_named_property 504 505Use **napi_has_named_property** to check whether an ArkTS object contains the specified property. 506 507CPP code: 508 509```cpp 510#include "napi/native_api.h" 511 512static napi_value NapiHasNamedProperty(napi_env env, napi_callback_info info) 513{ 514 // Obtain the two parameters passed from ArkTS. 515 size_t argc = 2; 516 napi_value args[2] = {nullptr}; 517 const int32_t strLength = 32; 518 char strKey[strLength] = ""; 519 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 520 // Obtain the property name. 521 size_t keyLength; 522 napi_get_value_string_utf8(env, args[1], strKey, strLength, &keyLength); 523 // Check whether the object has the specified property and store the result in hasProperty. 524 bool hasProperty = false; 525 napi_status status = napi_has_named_property(env, args[0], strKey, &hasProperty); 526 if (status != napi_ok) { 527 napi_throw_error(env, nullptr, "napi_has_named_property failed"); 528 return nullptr; 529 } 530 // Convert the bool value to napi_value and return it. 531 napi_value result; 532 napi_get_boolean(env, hasProperty, &result); 533 return result; 534} 535``` 536 537API declaration: 538 539```ts 540// index.d.ts 541export const napiHasNamedProperty: (obj: Object, key:string) => boolean | void; 542``` 543 544ArkTS code: 545 546```ts 547import hilog from '@ohos.hilog' 548import testNapi from 'libentry.so' 549interface NestedObj { 550 nestedStr: string; 551 nestedNum: number; 552} 553class Obj { 554 str: string = ""; 555 num: number = 0; 556 bol: boolean = false; 557 nestedObj: NestedObj = { nestedStr: "", nestedNum: 0 }; 558} 559let obj: Obj = {str: "bar", num: 42, bol: true, 560 nestedObj: { nestedStr: "nestedValue", nestedNum: 123 }}; 561hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_named_property : %{public}s', testNapi.napiHasNamedProperty(obj, 'str')); 562hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_named_property : %{public}s', testNapi.napiHasNamedProperty(obj, 'nestedStr')); 563hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_named_property : %{public}s', testNapi.napiHasNamedProperty(obj, 'bol')); 564``` 565 566### napi_define_properties 567 568Use **napi_define_properties** to define multiple properties for an ArkTS object. 569 570CPP code: 571 572```cpp 573#include <string> 574#include "napi/native_api.h" 575 576static napi_value DefineMethodPropertiesExample(napi_env env, napi_callback_info info) 577{ 578 // Create a property value of the int32 type. 579 int32_t propValue = 26; 580 napi_value returnValue = nullptr; 581 napi_create_int32(env, propValue, &returnValue); 582 return returnValue; 583} 584// Define a getter callback. 585static napi_value GetterCallback(napi_env env, napi_callback_info info) 586{ 587 napi_value result; 588 const char *str = u8"World!"; 589 size_t length = strlen(str); 590 // Create property values. 591 napi_create_string_utf8(env, str, length, &result); 592 return result; 593} 594 595// Define a setter callback. 596static napi_value SetterCallback(napi_env env, napi_callback_info info) 597{ 598 // Obtain the parameters passed to setter. 599 size_t argc = 1; 600 napi_value argv[1] = {nullptr}; 601 napi_value result; 602 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); 603 std::string buf; 604 size_t length; 605 napi_get_value_string_utf8(env, argv[0], (char *)buf.c_str(), NAPI_AUTO_LENGTH, &length); 606 napi_create_string_utf8(env, buf.c_str(), length, &result); 607 return result; 608} 609static napi_value DefineMethodProperties(napi_env env, napi_callback_info info) 610{ 611 napi_value obj; 612 napi_create_object(env, &obj); 613 // Define the defineMethodPropertiesExample function for the obj object, define a variable in the defineMethodPropertiesExample function, and return the variable. When the obj object is called, the defineMethodPropertiesExample will be called. 614 napi_property_descriptor descriptor[] = { 615 {"defineMethodPropertiesExample", nullptr, DefineMethodPropertiesExample, nullptr, nullptr, nullptr, napi_default, nullptr}}; 616 napi_define_properties(env, obj, sizeof(descriptor) / sizeof(descriptor[0]), descriptor); 617 return obj; 618} 619static napi_value DefineStringProperties(napi_env env, napi_callback_info info) 620{ 621 napi_value obj; 622 napi_create_object(env, &obj); 623 // Create a property value of the string type. 624 napi_value string_value; 625 napi_create_string_utf8(env, "Hello!", NAPI_AUTO_LENGTH, &string_value); 626 napi_property_descriptor descriptor[] = { 627 {"defineStringPropertiesExample", nullptr, nullptr, nullptr, nullptr, string_value, napi_default, nullptr}}; 628 napi_define_properties(env, obj, sizeof(descriptor) / sizeof(descriptor[0]), descriptor); 629 return obj; 630} 631 632static napi_value CreateStringWithGetterSetter(napi_env env, napi_callback_info info) 633{ 634 napi_value obj; 635 napi_create_object(env, &obj); 636 // Define the getter function. 637 napi_value getterFn; 638 napi_create_function(env, nullptr, 0, GetterCallback, nullptr, &getterFn); 639 napi_set_named_property(env, obj, "getterCallback", getterFn); 640 // Define the setter function. 641 napi_value setterFn; 642 napi_create_function(env, nullptr, 0, SetterCallback, nullptr, &setterFn); 643 napi_set_named_property(env, obj, "setterCallback", setterFn); 644 // Define properties with getter and setter. 645 napi_property_descriptor desc = {"defineGetterSetter", nullptr, GetterCallback, SetterCallback, nullptr, obj, napi_enumerable, nullptr}; 646 napi_define_properties(env, obj, 1, &desc); 647 return obj; 648} 649``` 650 651API declaration: 652 653```ts 654// index.d.ts 655export class DefineMethodObj { 656 defineMethodPropertiesExample: Function; 657} 658export class DefineStringObj { 659 defineStringPropertiesExample: string; 660} 661export class DefineGetterSetterObj { 662 getterCallback: Function; 663 setterCallback: Function; 664} 665export const defineMethodProperties: () => DefineMethodObj; 666export const defineStringProperties: () => DefineStringObj; 667export const createStringWithGetterSetter: () => DefineGetterSetterObj; 668``` 669 670ArkTS code: 671 672```ts 673import hilog from '@ohos.hilog' 674import testNapi from 'libentry.so' 675// Define a property of the method type. 676hilog.info(0x0000, 'testTag', 'Test Node-API define_method_properties:%{public}d', testNapi.defineMethodProperties() 677 .defineMethodPropertiesExample()); 678// Define a property of the string type. 679hilog.info(0x0000, 'testTag', 'Test Node-API define_string_properties::%{public}s ', testNapi.defineStringProperties() 680 .defineStringPropertiesExample); 681// getter and setter. 682hilog.info(0x0000, 'testTag', 'Test Node-API get::%{public}s ', testNapi.createStringWithGetterSetter() 683 .getterCallback()); 684hilog.info(0x0000, 'testTag', 'Test Node-API setter::%{public}s ', testNapi.createStringWithGetterSetter() 685 .setterCallback('set data')); 686``` 687 688### napi_get_all_property_names 689 690Use **napi_get_all_property_names** to obtain all property names in an ArkTS object. 691 692CPP code: 693 694```cpp 695#include "napi/native_api.h" 696 697static napi_value GetAllPropertyNames(napi_env env, napi_callback_info info) 698{ 699 // obtain the parameter. 700 size_t argc = 1; 701 napi_value args[1] = {nullptr}; 702 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 703 704 // Obtain all property names of the given object. 705 napi_value result; 706 napi_status status = napi_get_all_property_names(env, args[0], napi_key_own_only, napi_key_writable, 707 napi_key_numbers_to_strings, &result); 708 // If the operation fails, throw an error. 709 if (status != napi_ok) { 710 napi_throw_error(env, nullptr, "Node-API napi_get_all_property_names fail"); 711 return nullptr; 712 } 713 714 return result; 715} 716``` 717 718API declaration: 719 720```ts 721// index.d.ts 722export const getAllPropertyNames : (obj: Object) => Array<string> | void; 723``` 724 725ArkTS code: 726 727```ts 728import hilog from '@ohos.hilog' 729import testNapi from 'libentry.so' 730try { 731 class Obj { 732 data: number = 0 733 message: string = "" 734 } 735 let obj: Obj = { data: 0, message: "hello world"}; 736 let propertyNames = testNapi.getAllPropertyNames(obj); 737 hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_all_property_names: %{public}s', JSON.stringify(propertyNames)); 738} catch (error) { 739 hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_all_property_names error: %{public}s', error.message); 740} 741``` 742 743To print logs in the native CPP, add the following information to the **CMakeLists.txt** file and add the header file by using **#include "hilog/log.h"**. 744 745```text 746// CMakeLists.txt 747add_definitions( "-DLOG_DOMAIN=0xd0d0" ) 748add_definitions( "-DLOG_TAG=\"testTag\"" ) 749target_link_libraries(entry PUBLIC libhilog_ndk.z.so) 750``` 751