Share via


Advanced Class Creation

When you define a JScript class, you can assign properties, and the defined class can subsequently inherit from other classes. Properties, which are class members similar to fields, provide greater control over how data is accessed. By using inheritance, a class can extend (or add behavior to) another class.

You can define a class so that instances of the class support expando properties. This means that class-based objects can have properties and methods dynamically added to the object. Class-based expando objects provide some of the same functionality as prototype-based objects.

Classes with Properties

JScript uses function get and function set statements to specify properties. You can specify either or both accessors to create read-only, write-only, or read-write properties, although write-only properties are rare and may indicate a problem with the design of the class.

The calling program accesses properties in the same way that it accesses fields. The main difference is that the getter and setter for the property are used to perform the access, whereas fields are accessed directly. A property enables the class to check that only valid information is being entered, to keep track of the number of times the property is read or set, to return dynamic information, and so on.

Properties are usually used to access private or protected fields of the class. Private fields are marked with the private modifier, and only other members of the class can access them. Protected fields are marked with the protected modifier, and only other members of the class or derived classes can access them. For more information, see JScript Modifiers.

In this example, properties are used to access a protected field. The field is protected to help prevent outside code from changing its value while allowing derived classes to access it.

class Person {
   // The name of a person.
   // It is protected so derived classes can access it.
   protected var name : String;

   // Define a getter for the property.
   function get Name() : String {
      return this.name;
   }

   // Define a setter for the property which makes sure that
   // a blank name is never stored in the name field.
   function set Name(newName : String) {
      if (newName == "")
         throw "You can't have a blank name!";
      this.name = newName;
   }
   function sayHello() {
      return this.name + " says 'Hello!'";
   }
} 

// Create an object and store the name Fred.
var fred : Person = new Person();
fred.Name = "Fred";
print(fred.sayHello());

The output of this code is:

Fred says 'Hello!'

When a blank name is assigned to the Name property, an error is generated.

Inheritance from Classes

The extends keyword is used when defining a class that builds on another class. JScript can extend most common-language specification (CLS)-compliant classes. A class defined using the extends keyword is called a derived class, and the class that is extended is called the base class.

In this example, a new Student class is defined, which extends the Person class in the previous example. The Student class reuses the Name property defined in the base class but defines a new sayHello method that overrides the sayHello method of the base class.

// The Person class is defined in the code above.
class Student extends Person {
   // Override a base-class method.
   function sayHello() {
      return this.name + " is studying for finals.";
   }
}

var mary : Person = new Student;
mary.Name = "Mary";
print(mary.sayHello()); 

The output of this code is:

Mary is studying for finals.

Redefining a method in a derived class does not change the corresponding method in the base class.

Expando Objects

If you just want a generic object to be expando, use the Object constructor.

// A JScript Object object, which is expando.
var o = new Object();
o.expando = "This is an expando property.";
print(o.expando);  // Prints This is an expando property.

If you want one of your classes to be expando, define the class with the expando modifier. Expando members can only be accessed using the index ([]) notation; they cannot be accessed using the dot (.) notation.

// An expando class.
expando class MyExpandoClass {
   function dump() {
      // print all the expando properties
      for (var x : String in this)
         print(x + " = " + this[x]);
   }
}
// Create an instance of the object and add some expando properties.
var e : MyExpandoClass = new MyExpandoClass();
e["answer"] = 42;
e["greeting"] = "hello";
e["new year"] = new Date(2000,0,1);
print("The contents of e are...");
// Display all the expando properites.
e.dump(); 

The output of this program is:

The contents of e are...
answer = 42
greeting = hello
new year = Sat Jan 1 00:00:00 PST 2000

See Also

Concepts

JScript Modifiers

Creating Your Own Classes

Other Resources

Class-based Objects

JScript Objects