Is there a jcript method for checking if an array has a certain element in it?

This is not Opus strictly speaking, so I will ask on this section.

I am at a loss with this Jscript language. All attempts to research something for it, inevitably leads me to JavaScript sources that have answers/solutions that Jscript does not have.

In this instance, I want to find out if a array has a element in it:

function OnInit(initData){
	initData.name = "test";
	initData.desc = "testing";
	var cmd = initData.AddCommand();
	cmd.name = "testCmd";
	cmd.method = "OnTestCmd";
	}
	
function OnTestCmd(scriptCmdData){
	DOpus.ReloadScript(Script.file)
	DOpus.ClearOutput();
	print(["Jenny", "Matilda", "Greta"].indexOf("Matilda"))     //Error: Object doesn't support this property or method (0x800a01b6)
	print(["Jenny", "Matilda", "Greta"].includes("Matilda"))   //Error: Object doesn't support this property or method (0x800a01b6)

    arr = new Array("Jenny", "Matilda", "Greta")
    print(arr.indexOf("Greta"))                  //Object doesn't support this property or method (0x800a01b6)
	}


	function print(str){
		DOpus.Output(str)
	}

I am told I can use includes() or indexOf() but neither seem to work

Looking at MS docs for Jscript, there does not seem be a includes() method but it does have indexOf() which just errors out.

Also for people who write JScript code regularly here, at what point did JScript stop keeping up with JavaScript? This would help me know what JavaScript documentation I can follow.

Any help would be greatly appreciated!

Yep.

Remove duplicate words from filenames (JScript array.indexOf error)

A loooonng time ago.

Here are the Windows Script Host / ActiveScript Reference Manuals

1 Like

It's time to get a physical book about JScript.. o)
More and more online resources are being removed from the interweb, but it always helps to add "-javascript" and "-nodejs" etc. to your search term, to explicitly exclude a lot of hits dealing with ES6+ language variants.

Another great help is using polyfills. You can almost always add a polyfill to your JS native object and upgrade your set of methods on specific objects. You can find an include() polyfill for an array here e.g., but there are many more websites which have polyfills for all kind of things and methods.

If you want the include() method on your array, it's as simple as adding this snippet to your code. A nice feature of the language if you ask me.. o) You are basically adding a static member / function to the Array class by adding functions to Array.prototype - any new Array instance will feature the include() method.

Array.prototype.includes = function (element, fromIndex = 0) {
    fromIndex = typeof fromIndex === "undefined" ? 0 : fromIndex;
    for (var i = fromIndex; i < this.length; i++) {
        if (this[i] === element) {
            return true;
        }
    }
    return false;
};

Make sure any polyfill you come up with does not make use of ES6+ language statements like "let" or other ES6+ language features. You can almost always replace "let" with "var", ALMOST. If you are using closures, you might not be able to just use "var" anymore (it affects scope), but then there is another workaround to get "let" handling with "var", by using a wrapping function. This is out of scope of this topic I guess, but just to let you know. o)

Assigning default values to parameters in the function declaration also is not supported by JScript, some modern polyfills seem to make use of that, you need to change these and handle any default values the old school way..

Instead of..

    var myFunc = function (element, fromIndex = 0) {
        .. 
    }

You do this:

    var myFunc = function (element, fromIndex) {
        fromIndex = typeof fromIndex === "undefined" ? 0 : fromIndex;
        .. 
    }

Once you have your set of polyfills ready, you can reuse them where ever you need them of course, you also accommodate to the older / restricted set of methods in JScript over time. A lot of the things added to javascript is "icing on the cake", you don't actually need it. The proof is in Typescript, which is able to transpile code to any variant of JScript / javascript, whether its old or new, your program will run just fine.

So, JScript.. it's a cool thing, it's build into Windows since the year 2000 or so, you can run any JScript with "cscript.exe" directly from the command line. So, Windows already had some kind of "nodejs" long before it was invented.

InternetExplorer also was the first browser coming up with AJAX calls by using XMLHttpRequest object, this was way before people came up with jQuery() and things. AJAX is what made the modern web possible (loading / refreshing only parts of a website). Some people argue, AJAX is what destroyed the world wide web.. I can agree to some aspects of that at least. o)

And now.. on with the scripting fun! o)

ps: Another fact only a few people know, modern applications like "VSCode", "Balena Etcher" etc., which are build with the Electron framework are basically just cross platform variants of an HTA. The Electron framework is basically a browser, it's rendering in HTML and using Javascript for the application logic. Windows already had this type of application for decades, they were called HTA, "Hyper Text Application". The former windows versions made use of HTAs for the "Add / remove programs" program e.g., you can still use it today. I made several little tools using HTA and these tools are tiny compared to a modern Electron application, because all you need is already on the Windows system (the HTML rendering / engine and the scripting part).

Windows is and was ahead of time in so many things! o)
Not sure where it is heading now, but we will see. o)

2 Likes

Also maybe read this, covering some JScript / javascript differences / basics here as well:

1 Like

For practicing JScript or testing out differences between modern javascript found in current browsers, v8, nodejs etc., it might be unnecessary to do this in a DO button, as the DO environment adds things and rules not to be found in plain JScript.

A nice text editor like EditPlus and a "User tool" to directly run the script interpreter of Windows "cscript.exe" with the script you are editing in the editor, can be quicker and is more bare bones than trying to get behind JScript in DO.

The CLI of DO is what comes closest to editing and tinkering with JScript directly. You can open it by pressing ">" in a file display and entering "CLI", then switch to JScript and run your code with F5. It is similar to using a dedicated text editor, but not the same. If you screw up, by coding an endless loop e.g., you need to kill DO, not just the the cscript.exe task launched from the standalone text editor. You also might miss some advanced text editing features, but it's also quite useful. You can see in my screenshot of it here, that I already have over 1000 lines of test code in there, it's getting used! o) By accident I scrolled down to some prototypes / polyfills which add some basic path handling to regular String objects:

This is my "cscript.exe" user tool definition in EditPlus, it's setup to also capture the script output and show it directly in the built in log output area in the bottom area.

EditPlus is not free to use after trial period, but it's worth the small money. I don't know any free text editor which offers the same experience and features as EditPlus does (like running an external interpreter on your script). Notepad++ only does it with the help of plugins and it felt very clunky, as it is in general (if you ask me, but I'm an EditPlus fanboy, so.. o).

This is just to give some options and maybe get going quicker..

1 Like

@lxp Thank you very much for that link, I had no idea about Opus providing alternative objects for JScript ones. It turned out to be just what I needed in this case!

@tbone
This is amazing. I really appreciate you for these comprehensive thoughts and answers. There answers for issues I was facing, that I did not even raise on this thread. And beyond that, things that are bound to save me and others a lot of heache.

Saved and will keep coming back to it, Cheers