Share via


Creating Your Own Objects with Constructor Functions

A powerful feature of JScript is the ability to define constructor functions to create custom prototype-based objects for use in your scripts. To create an instance of a prototype-based object, you first must define a constructor function. This process creates a new object and initializes it (creates properties and assigns an initial value). When completed, the constructor returns a reference to the constructed object. Inside the constructor, the created object is referred to with the this statement.

Constructors with Properties

The following example defines a constructor function for pasta objects. The this statement allows the constructor to initialize the object.

// pasta is a constructor that takes four parameters.
function pasta(grain, width, shape, hasEgg) {
   this.grain = grain;    // What grain is it made of?
   this.width = width;    // How many centimeters wide is it?
   this.shape = shape;    // What is the cross-section?
   this.hasEgg = hasEgg;  // Does it have egg yolk as a binder?
}

After defining an object constructor, you create instances of the object with the new operator. Here the pasta constructor is used to create spaghetti and linguine objects.

var spaghetti = new pasta("wheat", 0.2, "circle", true);
var linguine = new pasta("wheat", 0.3, "oval", true);

You can dynamically add properties to an instance of an object, but those changes affect only that one instance.

// Additional properties for spaghetti. The properties are not added
// to any other pasta objects.
spaghetti.color = "pale straw";
spaghetti.drycook = 7;
spaghetti.freshcook = 0.5;

If you want to add an extra property to all instances of the object without modifying the constructor function, you can add the property to the constructor's prototype object. For more information, see Advanced Object Creation (Visual Studio - JScript).

// Additional property for all pasta objects. 
pasta.prototype.foodgroup = "carbohydrates";

Constructors with Methods

It is possible to include methods (functions) in the definition of an object. One way to do this is to include a property in the constructor function that refers to a function defined elsewhere. Like the constructor functions, these functions also refer to the current object with the this statement.

The following example expands on the pasta constructor function defined above to include a toString method that will be called if the function displays the value of the object. (In general, JScript will use the toString method of an object when the object is used in a situation where a string is required. You rarely need to call the toString method explicitly.)

// pasta is a constructor that takes four parameters.
// The properties are the same as above.
function pasta(grain, width, shape, hasEgg) {
   this.grain = grain;    // What grain is it made of?
   this.width = width;    // How many centimeters wide is it?
   this.shape = shape;    // What is the cross-section?
   this.hasEgg = hasEgg;  // Does it have egg yolk as a binder?
   // Add the toString method (defined below).
   // Note that the function name is not followed with parentheses;
   // this is a reference to the function itself, not a function call.
   this.toString = pastaToString;
}

// The function to display the contents of a pasta object.
function pastaToString() {
   return "Grain: " + this.grain + "\n" +
          "Width: " + this.width + " cm\n" +
          "Shape: " + this.shape + "\n" +
          "Egg?:  " + Boolean(this.hasEgg);
}

var spaghetti = new pasta("wheat", 0.2, "circle", true);
// Call the method explicitly.
print(spaghetti.toString());
// The print statement takes a string as input, so it
//  uses the toString() method to display the properties
// of the spaghetti object.
print(spaghetti);

This displays the following output.

Grain: wheat
Width: 0.2 cm
Shape: circle
Egg?:  true
Grain: wheat
Width: 0.2 cm
Shape: circle
Egg?:  true

See Also

Other Resources

Prototype-based Objects

JScript Objects