VC ++ accessing JavaScript (3)-Traversing arrays in Javascript

Source: Internet
Author: User

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.

  1. Variant varparams; // This is an array of parameters passed in by the script.
  2. // If the input type is not vt_dispatch, the error message is returned.
  3. If (varparams. VT! = Vt_dispatch)
  4. Return e_invalidarg;
  5. // Obtain the idispatchex Interface
  6. Ccomptr <idispatchex> pdispex;
  7. Hresult hR = varparams. pdispval-> QueryInterface (iid_idispatchex, (void **) & pdispex );
  8. If (failed (HR ))
  9. Return e_invalidarg;
  10. // Enumerate all attributes
  11. Dispid;
  12. // Obtain the dispid of the first element
  13. HR = pdispex-> getnextdispid (fdexenumall, dispid_startenum, & dispid );
  14. While (hR = noerror)
  15. {
  16. // Element name
  17. Ccombstr bstrname;
  18. HR = pdispex-> getmembername (dispid, & bstrname );
  19. If (failed (HR ))
  20. Return e_fail;
  21. // Element value
  22. Ccomvariant vvalue;
  23. Dispparams dispparamsnoargs = {null, null, 0, 0 };
  24. HR = pdispex-> invokeex (dispid, locale_user_default,
  25. Dispatch_propertyget, & dispparamsnoargs,
  26. & Vvalue, null, null );
  27. If (failed (HR ))
  28. Return e_fail;
  29. // Obtain the dispid of the next Element
  30. HR = pdispex-> getnextdispid (fdexenumall, dispid, & dispid );
  31. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.