Microsoft® JScript™ Advanced Object Creation | JScript Tutorial Previous Next

##### Using Constructors to Create Objects

> In Microsoft JScript, you use constructors to create and build a class of objects. You invoke a constructor with the new statement. It returns whatever it constructs.

> The special case Function constructor lets you create functions that are anonymous. An anonymous function is one that does not have a name. You can use the Function constructor to build a function "on the fly", for example, as one of the instructions inside another function. Such a function, which is only called from the one location, doesn't need a name.

> In the following example, such an anonymous function generates one line of a "name-and-email-address" listing. It checks the value of the firstNameFirst variable to decide whether to put the first name or the last name first, and the value of the emailNameFirst variable to decide whether to put the name or the email address first. The example assumes that the values of firstNameFirst and emailNameFirst are set elsewhere.

> >     for (j = 1; j < addressList[length]; j++)
> >     {
> >     oneListingLine = new Function(emailNameFirst, firstNameFirst, addressList, j, theName = new Function(firstNameFirst, addressList, j, var theName=(addressList[j].firstName + addressList[j].lastName);
> >     if(firstNameFirst)
> >           {
> >           theName=(addressList[j].firstName + addressList[j].lastName);
> >           },) ;
> >     if (emailNameFirst)
> >           {
> >     theListing = addressList[j].emailName+ ":\t" + theName
> >           } else theListing = theName + ":\t" + addressList[j].emailName; return theListing;)
> >     document.write(oneListingLine + "<br>");
> >     }

##### Writing Constructors

> To write your own constructors, you use the this keyword within the constructor to refer to the newly-created object. The constructor initializes the object.

> Though the constructor in the next example starts at an index of 0, this is not required. You can start with a first index of 1 if, for example, you want a parameter that indicates the actual number of indexes of the array or object. In the example, it's called extent to distinguish it from the automatically maintained length parameter of the built-in Array( ) object). If you write code that adds properties to the array, you have to update the extent parameter (or your equivalent) because this parameter is not maintained by JScript. Notice that even this extremely simple example uses both object (dot) and array (bracket) notation styles to refer to the current object.

> >     function MakeStringArray(length) {
> >     this.extent = length;
> >     for (iNum = 0; iNum < length; i++) {
> >     this[iNum] = "";
> >     }
> >     }

> >     // Use the constructor to create and initialize an array.
> >     myStringArray = new MakeStringArray(63);

##### Using Prototypes to Create Objects

> When you write an object definition, you can use prototype properties to create properties that are held in common by all objects that are generated by the definition. Prototype properties are copied by reference into each object of a class, so they have the same value for all objects in the class. However, you can change the value of a prototype property in one object, and the new value overrides the default, but only in that one instance. Other objects that are members of the class are not affected by the change.

> Using this principle, you can define additional properties for objects that are part of the JScript language, which all have prototypes. For example, if you want a special constant for a calculation, and the constant is not among those provided in the Math and Number objects, you can define it yourself and then assign it their respective object prototypes, or the prototype property of your object class.

> >     Math.prototype.Avogadro = 6.0232E23;
> >     function howManyMolecules(wtGrams,molWt) {
> >     return ((wtGrams/molWt)*Math.prototype.Avogadro);
> >     }
> >     document.write("There are " + howManyMolecules(window.prompt("How many grams?",0),window.prompt
> >     ("What's the molecular weight?",0)) +
> >      " molecules in that amount.");

> Perhaps more to the point, you can define a function, assign it to String.prototype as a method, and use it on any string anywhere in your script. The following example assumes the existence of a Periodic Chart array called "theElements", defined elsewhere in the script, which contains symbols for the elements, their names, their atomic weights, and other relevant information about them.

> >     function atomName(theSymbol) {
> >     return(theElements[theSymbol].fullName);
> >     }

> >     String.prototype.atomName = atomName;

> >     function decodeFormula(theFormula) {
> >     var theCurrentPiece = "";
> >     var theDecodedFormula = "";
> >     for (i = 1; i = theFormula.length ; i++);
> >     if (theFormtheCurrentPiece
> >     // Code statements to separate the formula string into an array of symbols and numbers.
> >     // Loop through the formula array and assemble the decoded string. Each term is:
> >     theDecodedFormula += formula[n].number
> >     theDecodedFormula += " ";
> >     theDecodedFormula += formula[n].symbol.prototype.atomName;
> >     theDecodedFormula += " "
> >     // End of loop.

> >     return theDecodedFormula;
> >     }

> >     decodeFormula(window.prompt("Formula?","Al2O3"));

© 1998 Microsoft Corporation. All rights reserved. Terms of Use.