Share via


Advanced Object Creation (Visual Studio - JScript)

JScript supports inheritance with custom prototype-based objects. Through inheritance, prototype-based objects can share a common set of properties and methods that can be dynamically added or removed. Moreover, individual objects can override the default behavior.

Creating a Prototype-based Object

To create an instance of a prototype-based object, you first must define a constructor function. For more information, see Creating Your Own Objects with Constructor Functions. Once this constructor is written, you can use properties of the prototype object (which is itself a property of every constructor) to create inherited properties and shared methods. The constructor provides the instance-specific information to an object, while the prototype object provides the object-specific information and methods to the object.

Note

To affect all instances of the object, there must be a change to the prototype object of the constructor. Changing the prototype property of one instance of an object has no effect on the other instances of the same object.

Since the properties and methods of the prototype object are copied by reference into each instance of an object, all instances have access to the same information. You can change the value of a prototype property in one instance to override the default, but the change will affect only that one instance. Here is an example that uses the custom constructor, Circle. The this statement enables the method to access members of the object.

// Define the constructor and add instance specific information.
function Circle (radius) {
    this.r = radius;  // The radius of the circle.
}
// Add a property the Circle prototype.
Circle.prototype.pi = Math.PI;
function aCirclesArea () {
   // The formula for the area of a circle is pi*r^2.
   return this.pi * this.r * this.r; 
}
// Add a method the Circle prototype.
Circle.prototype.area = aCirclesArea;
// This is how you would invoke the area function on a Circle object.
var aCircle = new Circle(2);
var a = aCircle.area();

Using this principle, you can define additional properties for existing constructor functions (which all have prototype objects). This will work only when fast mode is turned off. For more information, see /fast.

For example, if you want to remove leading and trailing spaces from strings (similar to the Trim function in Visual Basic), you can create your own method on the String prototype object, and all strings in your script will automatically inherit the method. This example uses a regular expression literal to remove the spaces. For more information, see Regular Expression Object.

// Add a function called trim as a method of the prototype 
// object of the String constructor.
String.prototype.trim = function() {
   // Use a regular expression to replace leading and trailing 
   // spaces with the empty string
   return this.replace(/(^\s*)|(\s*$)/g, "");
}

// A string with spaces in it
var s = "    leading and trailing spaces    ";
print(s + " (" + s.length + ")");

// Remove the leading and trailing spaces
s = s.trim();
print(s + " (" + s.length + ")");

After compiling this program with the /fast- flag, the output of this program is:

    leading and trailing spaces     (35)
leading and trailing spaces (27)

See Also

Other Resources

Prototype-based Objects

JScript Objects