We often encounter multiple parameters defined in the script packaged into an array, and then passed the array to a component method. JavaScript and VBScript define arrays in different ways. Arrays in VBScript use the automatic compatibility type.SafearrayThe ccomsafearray class in VC ++ can be used for convenient traversal. Next we will focus on how to traverse arrays in JavaScript.
I. array definition in JavaScript scripts
Arrays in JavaScript are more like objects. In JavaScript, you can assign values to an array element by Numerical index, or assign values to an array element by name index, as shown below:
VaR oparams = new object ();
Oparams ["loginname"] = "test ";
Oparams ["password"] = "123 ";
Oparams ["verifycode"] = "12pq ";
In this way, each element can identify a specific meaning.
2. In the previous article, we said that the idispatchex interface is exposed to the object, and we can useGetnextdispid,GetmembernameAndInvokeexThe three methods are used to traverse the array.
- Variant varparams; // This is an array of parameters passed in by the script.
- // If the input type is not vt_dispatch, the error message is returned.
- If (varparams. VT! = Vt_dispatch)
- Return e_invalidarg;
- // Obtain the idispatchex Interface
- Ccomptr <idispatchex> pdispex;
- Hresult hR = varparams. pdispval-> QueryInterface (iid_idispatchex, (void **) & pdispex );
- If (failed (HR ))
- Return e_invalidarg;
- // Enumerate all attributes
- Dispid;
- // Obtain the dispid of the first element
- HR = pdispex-> getnextdispid (fdexenumall, dispid_startenum, & dispid );
- While (hR = noerror)
- {
- // Element name
- Ccombstr bstrname;
- HR = pdispex-> getmembername (dispid, & bstrname );
- If (failed (HR ))
- Return e_fail;
- // Element value
- Ccomvariant vvalue;
- Dispparams dispparamsnoargs = {null, null, 0, 0 };
- HR = pdispex-> invokeex (dispid, locale_user_default,
- Dispatch_propertyget, & dispparamsnoargs,
- & Vvalue, null, null );
- If (failed (HR ))
- Return e_fail;
- // Obtain the dispid of the next Element
- HR = pdispex-> getnextdispid (fdexenumall, dispid, & dispid );
- }