Creating Your Own Objects

Microsoft Office JScript

Microsoft® JScript® Creating Your Own Objects
 JScript Tutorial; 
 Previous 


To create instances of an object, you must first define it by giving it properties and, if appropriate, methods. For instance, the following example defines a pasta object. Notice the keyword this, which you use to refer to the current object.
function pasta( grain, grain2, width, shape, shapenum, extent, egg )
{
  this.length = 7;   // Number of properties in the object, not including this one.
  this.grain = grain;   // What grain is it made of? (string)
  this.grain2 = grain2;   // Any other flour in it? (string)
  this.width = width;     // How wide is it? (number)
  this.shape = shape;   // What is the cross-section? (string)
  this.shapenum = shapenum;  // Is it one of the registered shapes? (number)
  this.extent = extent;    // How long is it? (number)
  this.egg = egg;  // Does it have egg yolk as a binder? (Boolean)
}
Once you define an object, you create instances of it with the new operator.
var spaghetti = new pasta("wheat", "", 0.2, "circle", 9, 30, true);
var linguine = new pasta("wheat", "", 0.3, "oval", 17, 30, true);
You can add properties to one instance of an object, to change that instance, but those properties do not become part of the definition of the object, and do not show up in other instances unless you specifically add them. If you want the extra properties to show up in all instances of the object, you must add them to the object definition.
// Additional properties for spaghetti.
spaghetti.color = "pale straw";
spaghetti.drycook = 7;
spaghetti.freshcook = 0.5;

var chowFun = new pasta("rice", "", 3, "flat", , 12, false); 
/*
Neither the chowFun object, the linguine object, nor the pasta object definition
has the three extra properties given to the spaghetti object.
*/
Including Methods in the Definition
It is possible to include methods in the definition of an object. The following example builds an object that consists of an array of strings, and a method. The method adds a string to the array, increasing its size in order to do so. Notice that this makes each instance of the object indefinitely extensible.
function addItem(newItem)  // Define a function to extend the list.
{
    this.length += 1;  // Increment the length of the array.
    this[(this.length-1)] = newItem;  // Add the new item, maintaining item numbering.
}

function shoppingList(firstItem) // Define a "shopping list" object.
{
    this.length = 2;  // Number of properties in the object, not including this one.
    this.addItem = addItem;   // Include the addItem function as a method.
    this[(this.length-1)] = firstItem;  // The first item is numbered 1.
}

var myList = new shoppingList("Milk");
myList.addItem("Eggs");  // Use the method to add Eggs, which become item 2.
myList.addItem("Breadfruit");  // Breadfruit becomes item 3.
At this point, the contents of the array are as follows:
  • myList[length] is 4
  • myList[addItem] is the addItem function
  • myList[1] is Milk
  • myList[2] is Eggs
  • myList[3] is Breadfruit
Note that the indexing is not exactly as you might expect it to be if it were handled in a strictly numeric way. If you execute a for...in loop on this array, the loop iterates in the order given here, and the loop variable has the initial value "length" rather than 0.