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