How to create javaScript like objects in VBA

In javaScript, everything is an object. Classes don’t really exist  because they don’t have to. To create an class like structure, you just do something like this

 

Then you can access employee.id etc just like the properties of a class. Another way to do this is to use a jSon string like this.

 

All this is very useful for creating adHoc type objects. VBA has classes, but you can’t just throw something together at run time as you do in javaScript.

The cJobject class was originally created to be able to deserialize/ serialize jSon, but they also can be used to act like javaScript objects – meaning that you don’t need to bother with property only classes. Here’s an example of creating a cJobject from jSon, just like in javaScript.

 

Of course you might not have the jSon string, and need to build it up a piece at a time – here’s how.

 

And when you serialize it you get a nice jSon string …..

 

And of course you can access the objects directly, for example.

 job.find(“John”).child(“grade”).Value

For more information on this, see How to use cJobject

About brucemcp 223 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


10 + 8 =