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