[This is preliminary documentation and subject to change]
Declares the name of a class, as well as a definition of the variables, properties, and methods that comprise the class.
[attributes] [modifiers] class classname [extends baseclass][implements interfaces]{
[classmembers]
}
The classmembers may be any of the following. For methods declarations:
[attributes] [modifiers] function fname(parmlist) [: type] {
[body]
}
For property declarations:
[attributes] [modifiers] function accessor pname(parmlist) [: type] {
[body]
}
For field declarations:
[attributes] [modifiers] var vname [: type] [= value]
For class initializers:
static {
[statements]
}
attributes
Optional. One or more of the following that indicate the attributes of the class or its members:
Attribute | Valid for | Meaning |
---|---|---|
expando | class | The class is given a default indexed property that can store and retrieve dynamic properties (expandos). |
compatible | class | The class is made an expando class, and can also access class members with square brackets. A compatible class cannot use all the features of a normal class. |
override | method or property | Member overrides a member with the same name in the base class. This is the default. |
hide | method or property | Member does not override a member with the same name in the base class. |
clsCompliant | class, method, property, or field | Forces the compiler to ensure that the member is CLS compliant. Takes a single argument (true or false) to turn on or off the check. |
any NGWS attribute | As applicable | Any other attributes defined by NGWS can be used JScript classes, methods, properties, and fields. |
modifiers
Optional. One or more of the following that indicate the visibility of the class or its members:
Modifier | Valid for | Meaning |
---|---|---|
public | class, method, property, or field | Member is visible everywhere. No restriction is placed on this member's visibility. |
package | class, method, property, or field | Member is visible everywhere within the package in which it is declared. It is not visible outside the package. |
protected | method, property, or field | Member is visible only within the class in which it is declared, and any subclasses of that class. |
private | method, property, or field | Member is visible only within the class in which it is declared. It is not visible to subclasses. |
Static | method, property, or field | For methods, indicates that it can be called without an instance of the class. For properties and fields, indicates that one copy is shared by all instances. Note that static is also used to introduce a static initializer. |
Abstract | class, method, or property | For methods or properties, indicates that the member does not have an implementation. For classes, indicates that there is one or more unimplemented method. An abstract class, or a class that contains an abstract member, cannot be created, but it can be used as a base class. |
classname
Required. Name of the Class; follows standard variable naming conventions.
extends
Optional. Keyword indicating that the named class extends, or adds members to, a previously defined class of the same name. If this keyword is not used, a standard JScript base class is created.
baseclass
The name of an existing class being extended.
implements
Optional. Keyword indicating that the class implements one or more interfaces.
interfaces
A comma-delimited list of interface names. Every member of every implemented interface must be given an implementation of the class. If two or more interfaces share the same name, they will also share the same implementation.
classmembers
Optional. classmembers can be method declarations, property declarations, field declarations, or nested class declarations.
fname
Required. Name of the function being created. Must be unique within the class.
parmlist
Optional. Formal parameter list for the function. May optionally include a type specification for each parameter.
type
Optional. Return type of the function.
body
One or more statements that are the code of the entity being declared. Note that static methods can only access other static members (fields and methods).
accessor
Required. May be either get for readable property definitions, or set for writable property definitions. The set method cannot have a return type.
pname
Required. Name of property being created. Must be unique within the class except the same pname can be used with both accessor keywords to identify readable and writable properties.
vname
Required. Name of variable being created.
value
Optional. Value being assigned to vname.
static
Optional. Keyword indicating that the code enclosed within the initializer block is executed upon first reference to the class or a member of the class. Only other static members can be accessed inside the static initializer. More than one static initializer can be specified for a given class but they are executed in the order in which they appear.
statements
Optional. The code that comprises the initializer block.
Instances of a class are created using the new operator. If a class is abstract or has abstact members, it cannot be created with the new operator. You must create a subclass that implements the abstract methods.
A class can expose one or more declared properties. Properties can be read-only, write-only, or read-write depending on the combination of get and set property accessors defined within the class. A property can take any arbitrary number of arguments although, if both and get and set accessor are defined, the argument list must match in number, type , and order, except that the set accessor must have one additional argument. This additional argument must be specified at the end of the argument list and must be of the same return type as the get accessor. Therefore, a set accessor must always have a least one argument, and may not specify a return type, while a get accessor must always specify a return type.
By default, JScript 7.0 classes do not support expando properties. To allow a class to handle dynamically added properties, use the expando attribute, which automatically creates a default indexed property for the class. Expando properties are accessed using square brackets or parentheses. Note that you but you cannot access non-expando members of the class using square brackets or parentheses.
To make a JScript 7.0 class behave the same way as objects in previous JScript versions, where square brackets and the dot operator are used to access expando properties and class members, use the compatible attribute for the class. This allows old-style behavior at the expense of some new JScript features, such as visibility modifiers and versioning.
A class extends a base class using the extends keyword, where the new class inherits the functionality of the existing class. The new class accesses members of the base class using the super keyword. Methods of the base class are overridden by declaring a new function with the same name and parameters. You cannot declare a function with the same name and different parameters in JScript.
A class implements one or more interfaces using the implements keyword. Implementing an interface does not inherit any behavior, but rather marks the class as supporting a well-known 'signature' that can be used when interacting with other classes. Every method that is defined in an interface being implemented by a class must be defined in the class.
The following example creates a CPerson class with various fields and methods, the details of which have been omitted. A CCustomer class is derived from CPerson, having additional fields and methods not applicable to a generic member of the CPerson class.
// All members of CPerson are public by default.
class CPerson{
var name : String;
var address : String;
// CPerson constuctor
function CPerson(name : String){
this.name = name
};
// printMailingLabel is an instance method, as it uses the
// instance's name and address information.
function printMailingLabel(){
...
};
// printBlankLabel is static as it doesn't require
// any person-specific information.
static function printBlankLabel(){
...
};
// Print a blank mailing label. Note that no CPerson object
// exists at this time.
CPerson.printBlankLabel();
// Create a CPerson object and add some data.
var John : CPerson = new CPerson("John Doe");
John.address = "15 Broad Street, Atlanta, GA 30315";
// Print a mailing label with John's name and address.
John.printMailingLabel();
// Create an extension to CPerson.
class CCustomer extends CPerson{
// The customer's billing address.
var billingAddress : String;
// Last order.
var lastOrder : String;
// Constructor for this class.
function CCustomer(name : String){
// Call superclass constructor.
super(name);
};
// Customer's credit limit. This is a private field
// so that only member functions can change it.
// A public method is needed to read the credit limit.
private var creditLimit : double;
function getCreditLimit() : double{
return creditLimit;
}
}
// Create a new CCustomer.
var Jane : CCustomer = new CCustomer("Jane Doe");
// Do something with it.
Jane.billingAddress = Jane.address = "12 Oak Street, Buffalo, NY 14201";
Jane.lastOrder = "Windows 2000 Server";
// Print the credit limit.
print(Jane.name + "'s credit limit is " + Jane.getCreditLimit());
// Call a method defined in the base class.
Jane.printMailingLabel();