JScript Language Elements
| Tutorial | Reference |
Creating Your Own Objects
| Objects | Built-In Objects | Creating Your Own Objects |
Before you can create instances of an object, you must first define it. The definition of an object looks like the definition of a function. (Note, in the following example, the use of the keyword this, which refers to the current object.)
EXAMPLE:
function pasta( grain, grain2, width, shape, shapenum, extent, egg ) // define a "pasta" object { 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; // Other flour in it? (string) this.width = width; // How wide is it? (number, for our purposes given in centimeters) this.shape = shape; // What cross-section? (string) this.shapenum = shapenum; // Is it one of the registered shapes? (number) this.extent = extent; // How long? (number, for our purposes given in centimeters) this.egg = egg; // Does it have eggyolk in it as a binder? (boolean) }Once you've defined an object, you create instances of it with the new statement.
EXAMPLE:
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, 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 for all instances of the object, you must add them to the definition.
EXAMPLE:
// (...continuing after the previous example) spaghetti.color = "pale straw"; spaghetti.drycook = 7; spaghetti.freshcook = 0.5; // The spaghetti object has three more properties now. var cutupchowfun = new pasta("rice", "", 3, "flat", , 12, false); // The "chow fun, cut to size" object does not have the three extra // properties, nor does the linguine object, nor the pasta object definition.Including functions as methods of objects
It is possible to include methods in the definition of an object. The methods are functions, defined in the ordinary way, which are then included as properties of the object in its definition.
EXAMPLE:
function pastaLister(pastaName) { document.write("_" + pastaName + "_<br>"); document.write("Main grain: " + this.grain + "<br>"); document.write("Second grain: " + this.grain2 + "<br>"); document.write("Width, cm: " + this.width + "<br>"); document.write("Cross-section: " + this.shape + "<br>"); document.write("Shape number: " + this.shapenum + "<br>"); document.write("Length, cm: " + this.extent + "<br>"); document.write("Binder?: " + this.egg + "<br>"); document.write("<br>"); } function pasta( grain, grain2, width, shape, shapenum, extent, egg ) { // define a "pasta" object this.length = 8; // Number of properties in the object, not including this one this.grain = grain; // What grain is it made of? (string) this.grain2 = grain2; // Other flour in it? (string) this.width = width; // How wide is it? (number) this.shape = shape; // What cross-section? (string) this.shapenum = shapenum; // Is it one of the registered shapes? (number) this.extent = extent; // How long? (number) this.egg = egg; // Does it have eggyolk in it as a binder? (boolean) this.pastaLister = pastaLister; // Include the "about this pasta" function as a method. } var spaghetti = new pasta("wheat", "", "0.2", "round", 9, 30, true); var linguine = new pasta("wheat", "", 0.3, "oval", 17, 30, true); spaghetti.pastaLister("Spaghetti"); linguine.pastaLister("Linguine"); // ...And so on.
© 1996 by Microsoft Corporation.