Creating a jscript variable as an object

As I learn Jscript I have used this variable creation and it has puzzled me because it has "new":

var fso = new ActiveXObject("Scripting.FileSystemObject");

I think I've found what it is - the new means an object is created, but I can't understand why or when this should be done. Whilst the basic jscript language is in essence fairly straightforward (if, while, loop, var, etc) the challenge for me has been the interaction with dopus objects and the massive set of other objects out there.

Here's another one from one of my recent scripts:

var folderPath = new String(enumFiles.item().realpath); //get folderPath as jscript string

In this case, this statement is inside a while loop, so ideally I would want to declare it before the while, then use it in the loop. So the statement is doing 3 things:
[ul]

  1. create a variable
  2. assign its value - convert a dopus property into a string
  3. make it an object
    [/ul]

I wrote the following to your "how to save selected files" thread, but did not hit "submit" o):
Wondered why you ask some things I already tried to answer. o)

Preventing undeclared variables:
Not in JScript, but in Javascript (which does not harm if used in JScript at least): stackoverflow.com/questions/6133689

The "new" keyword is needed when creating new instances of objects and is kind of always required when creating ActiveX-objects. The latter are basically libraries (*.dll), that can be used in nearly every environment/language, "new" is also required to create instances of objects you defined within your own jscript code.

If you make use of methods on already created or existing (static) objects (the Resolve() case), then no "new" is required, as you're already dealing with a proper object (-instance, if not static).

Thanks tbone.

I tried:

"use strict";
doesNotExist = 42; // this throws a ReferenceError

from your url in the CLI and nothing was reported.

Does it work for you, although I realise you don't need it!

Thanks for explaining about "new". I'm at least aware of the problem now, even though I am unlikely to get it right first time.

If I wanted to split up the folderPath example, because although it works, it isn't best practice to keep defining the same variable in the loop:

  1. ouside the while loop - var folderPath = new String(""); // define object
  2. inside the loop - folderPath = String(enumFiles.item().realpath); //get folderPath as jscript string

[quote]I tried:
"use strict";
doesNotExist = 42; // this throws a ReferenceError
from your url in the CLI and nothing was reported.[/quote]
That's the expected result, because it has no effect in jscript. o)
I just wanted to mention that it is available in javascript at least. Both are quite similar but still differ in minor details.

You're right, but I think it makes no noticeable difference in execution times/memory consumption (still worth a test), so I wouldn't care too much if "var" is used in loops. On the win-side: Fewer lines of code and easier to read.

This should be the way if you want your variable to be declared prior to entering the loop.

var folderPath=""; while(1){ folderPath = new String(enumFiles.item().realpath); }
Consider reading this regarding (new) String(..): stackoverflow.com/questions/5750656

The new keyword: I would not try to get a perfect understanding of it in the early jscript-learning phase. It can be needed, it can be omitted at times and it can even be replaced with Object.create(MyObj) in many situations. With objects from DO mixing in, which never require "new" to get a new Command, Vector and whatever instance, I'd just stick to where examples make use of "new" to determine if it's required. I admit, I also couldn't explain why it's needed for ActiveXObject("Scripting.FilesystemObject") and not for "DOpus.Create.Command()", it probably depends on how these components were implemented.

"new" should be safe to use on native/selfmade jscript objects if you request new object instances though.