Scriting: How to use Maps in JS?

I feel a little stupid asking this, but... How do I add an element to a JS Map?

In VBS this works just fine:

Set m = DOpus.Create.Map m("key1") = "val1" DOpus.Output(m("key1"))

In JS, however, this doesn't work:

var m = DOpus.Create.Map(); m["key1"] = "val1";
There's an error in line 2: Object doesn't support this property or method.

The same happens with:

var m = DOpus.Create.Map(); m.key1 = "val1";

And if I try:

var m = DOpus.Create.Map("key1", "val1"); DOpus.Output("key1 = " + m.key1);
...then key1 is undefined.

??? What am I not getting?? :angry:
Thanks,
MartO

It's the same in JScript as in VBScript.

VBScript:

Set m = DOpus.Create.Map("key1", "val1") m("key2") = "val2" DOpus.Output("key1 = " & m("key1")) DOpus.Output("key2 = " & m("key2"))

JScript:

var m = DOpus.Create.Map("key1", "val1"); m("key2") = "val2"; DOpus.Output("key1 = " + m("key1")); DOpus.Output("key2 = " + m("key2"));

I guess that's the same trap I got into when starting with DOs scripting objects. You probably also ignore the unlikely variation, where it looks like you would assign a value to a method-call, which is something I've rarely seen (well, never), but for DO that's how it works. DOs scripting objects and methods are supernatural! o)

More obvious is, that the known handling for arrays/objects to index into with square brackets [] or use "." to access an assoc. array-key, does not work for DOs objects, as they are non-native js objects.

Try this:

var map = DOpus.Create.Map(); map("key01") = "value01"; DOpus.Output("key01 = " + map("key01") );
If you need a native associative array in js, you can do:

var assocArray = new Array(); assocArray["key01"] = "value01"; DOpus.Output("key01 = " + assocArray["key01"] ); DOpus.Output("key01 = " + assocArray.key01 );

Thanks a lot to you both. I'll try to remember that...

Yes, of couse I fell into the trap of this special (and slightly confusing) DOpus syntax; I automatically expected a Map to work like an associative array.
You live an learn... :wink:

It's not really special to Opus.

Opus doesn't provide a JavaScript library, or anything specific to JScript, so its objects won't necessarily use the same syntax as what you're used to if you've only used JavaScript libraries and built-in functionality before.

Opus provides COM objects which any ActiveScripting language can consume. Those will obviously be slightly different to what you're used to in places, unless you've worked with similar objects when interfacing with other tools.

I'm guessing most JavaScript programmers have mainly used it in web pages, where everything is native JavaScript, but now you're using it to talk to COM objects which are different beasts.

Yes, you're right, of course. Particularly this...

...is something I hadn't really wrapped my head around yet. I'm getting there... :wink:

Yes, yes! o)
Please notice that this fact does not yet explain, why we can use square bracket syntax on OnInitData.config, a non native js-object provided by DO:

onInitData.config["DebugOutput"] = false;

Whereas DOpus.vars and some others, do not allow quare brackets and require curved brackets instead:

DOpus.vars("myvar") = "hello!";

I already metioned that I don't get how this works, because DOpus.vars("myvar") looks like a method call and I've never seen a value assigned to a method call before. I especially searched the net to backup my experience on this and did find nothing. I also used symbolhound.com to find code matching that pattern, no hits.

If you look at any COM object provided by microsoft or some freeware, the commonly used syntax to handle DOpus.vars("myvar") = "hello!"; is:

DOpus.vars.Add("myvar", "hello!"); //or something like DOpus.vars.Add( NewVar("myvar", "hello!") );
This should look very familiar to coders and also seems available for DO, because this pattern is already used for DOpus.vars.Set("myvar", "hello!") e.g.

I'm just sharing my long term experience on this, take it fwiw to you. I felt a bit stronger for a second, because MartO also had some difficulties with that syntax and so I wanted to take the chance to tell again. o) Creating bigger object models is a challenge, so is naming things and finding repetitive patterns. Scripting in DO is still a new born and why not have some adjustments while the amount of scripts to adapt is limited.

I seriously offer to voluntarily adapt all scripts in the forum! o) And now I'm out, thanks for your patience and attention! o)