1# Setting the private Property Using JSVM-API 2 3## Introduction 4 5This topic walks you through on how to use JSVM-API to set a private property for an object. 6 7## Basic Concepts 8 9In JSVM-API, you can create private keys, use the keys to create and delete properties of objects, and persist private key symbols. 10 11## Available APIs 12 13| API | Description | 14|----------------------------------------|--------------------------------| 15| OH_JSVM_CreateDataReference | Creates a data reference with a specified reference count in JSVM.| 16| OH_JSVM_GetReferenceData | Checks whether a specified reference is valid. If the reference is valid, the JavaScript data associated with the reference is returned; otherwise, **result** is set to **NULL**.| 17| OH_JSVM_CreatePrivate | Creates a JS private key object. | 18| OH_JSVM_SetPrivate | Sets a private property for a passed-in object. | 19| OH_JSVM_GetPrivate | Obtains the private property of an object based on the private key. | 20| OH_JSVM_DeletePrivate | Deletes the private property of an object based on the private key. | 21 22## Example 23 24If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ code involved in manipulating private properties. 25 26### Creating a private Key, adding a private Property, and Deleting the Property 27 28CPP code: 29 30```cpp 31static JSVM_Value privateTest(JSVM_Env env, JSVM_CallbackInfo info) { 32 JSVM_VM vm; 33 JSVM_HandleScope outerScope; 34 OH_JSVM_GetVM(env, &vm); 35 OH_JSVM_OpenHandleScope(env, &outerScope); 36 37 JSVM_HandleScope handleScope; 38 JSVM_Data privateKey; 39 JSVM_Value object; 40 JSVM_Value property; 41 JSVM_Ref privateRef; 42 { 43 OH_JSVM_OpenHandleScope(env, &handleScope); 44 OH_JSVM_CreateObject(env, &object); 45 OH_JSVM_CreatePrivate(env, nullptr, &privateKey); 46 OH_JSVM_CreateInt32(env, 1, &property); 47 OH_JSVM_SetPrivate(env, object, privateKey, property); 48 OH_JSVM_GetPrivate(env, object, privateKey, &property); 49 int propertyValue = 0; 50 OH_JSVM_GetValueInt32(env, property, &propertyValue); 51 OH_LOG_INFO(LOG_APP, "private property set: %{public}d\n", propertyValue); 52 OH_JSVM_DeletePrivate(env, object, privateKey); 53 OH_JSVM_GetPrivate(env, object, privateKey, &property); 54 bool isUndefined = false; 55 OH_JSVM_IsUndefined(env, property, &isUndefined); 56 OH_LOG_INFO(LOG_APP, "private property deleted is undefined: %{public}d\n", isUndefined); 57 OH_JSVM_CreateDataReference(env, privateKey, 1, &privateRef); 58 OH_JSVM_CloseHandleScope(env, handleScope); 59 } 60 { 61 OH_JSVM_OpenHandleScope(env, &handleScope); 62 OH_JSVM_GetReferenceData(env, privateRef, &privateKey); 63 OH_JSVM_CreateObject(env, &object); 64 OH_JSVM_CreateInt32(env, 2, &property); 65 OH_JSVM_SetPrivate(env, object, privateKey, property); 66 OH_JSVM_GetPrivate(env, object, privateKey, &property); 67 int propertyValue = 0; 68 OH_JSVM_GetValueInt32(env, property, &propertyValue); 69 OH_LOG_INFO(LOG_APP, "second private property set: %{public}d\n", propertyValue); 70 OH_JSVM_CloseHandleScope(env, handleScope); 71 } 72 73 OH_JSVM_CloseHandleScope(env, outerScope); 74 return nullptr; 75} 76 77static JSVM_CallbackStruct param[] = { 78 {.data = nullptr, .callback = privateTest}, 79}; 80 81static JSVM_CallbackStruct *method = param; 82 83// Alias for the wrapperObject method to be called from JS. 84static JSVM_PropertyDescriptor descriptor[] = { 85 {"privateTest", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 86}; 87 88// Call the C++ code from JS. 89const char *srcCallNative = R"JS(privateTest();)JS"; 90``` 91 92## Expected Result 93``` 94private property set: 1 95private property deleted is undefined: 1 96second private property set: 2 97``` 98