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:
create a variable
assign its value - convert a dopus property into a string
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).
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)
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.
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.