Home > Articles > Home & Office Computing > Microsoft Windows Vista & Home Server

  • Print
  • + Share This
This chapter is from the book

Using Objects with JScript

JScript, like VBScript, is a strongly object-oriented language—it's expected that programmers will use objects to extend its power. JScript supplies 11 built-in object types, and programmers can create generic and structured object types in scripts. I don't discuss the intrinsic object types here because this book focuses on external scripting and Windows management objects.

External COM/ActiveX objects are created using the new statement, as follows:

   variablename = new ActiveXObject("objectname");

Here, variablename is the declared variable that is to receive the new object reference.

After you have an object variable in hand, its methods and properties are accessed using variable . propertyname or variable . methodname . For example, this is a short script that tells whether your C: drive has a folder named \windows:

   var fso;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   if (fso.FolderExists("c:\windows"))
      WScript.echo("There is a folder named c:\\windows");

Parentheses must be used on all method calls, even if the return value is not used—VBScript tolerates a statement such as

   WScript.echo "There is a folder named c:\windows"

but JScript does not.

Case Sensitivity

In the WSH environment, JScript programs have access to a predefined object named WScript, which provides several useful methods and properties pertaining to the script's environment, execution, and debugging. Because JScript is a case-sensitive language, to use this object you must type WScript with a capital W, exactly as written here.

However, the method and property names of ActiveX and COM objects are not case sensitive. For example, JScript permits you to type WScript.echo or WScript.Echo.

Working with Collections

If you are used to using JScript with Internet Explorer in browser or server-side scripts, you might be familiar with objects that return collections of objects. Many Internet Explorer objects let you scan through the object collection using JScript's for...in statement.

However, most other objects' collections do not work with for...in and you must use an Enumerator object to work with them. This is true of most objects you encounter in the WSH environment. JScript's Enumerator object gives you a way of accessing a collection by stepping forward or backward through the collection's list of objects.

To use a collection provided by a scripting or other ActiveX object, you must first convert it to an enumerator:

   enumObject = new Enumerator(collectionObject);

The Enumerator object has no properties (in particular, no Length property; if you need to know the number of items, you must get it from the original collection's Country property). It does have an internal concept of its "position" in the collection and has methods that let you move the current position forward or back to the beginning. Its four methods are listed in Reference List 3.1.

Reference List 3.1. Methods of the JScript Enumerator Object


Returns the current item from the collection. The return value is an object of whatever type the collection holds. If the collection is empty or the current position is undefined, it returns undefined.


Returns a Boolean value: True if the current item is the last in the collection, if the current position is undefined, or if the collection is empty. Otherwise, False is returned.


Makes the first item in the collection the current item. If the collection is empty, atEnd is immediately True.


Makes the next item in the collection the current item. If the collection is empty or the current item is already the last in the collection, item is undefined.

Although a newly created enumerator should be positioned on the first item automatically, it's a better style to use moveFirst before examining atEnd or item.

Here's an example. This script lists the names of all the files in the root folder on the C: drive:

   var fso, e, file;

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

   e = new Enumerator(fso.GetFolder("c:\\").files);
   for (e.moveFirst(); ! e.atEnd(); e.moveNext()) {
       file = e.item();

Now, if you plan on writing scripts only in JScript, you can skip ahead to the section titled "Using the WScript Object" for more information about the built-in WScript object.

  • + Share This
  • 🔖 Save To Your Account