Specifications
Example:
class Area {
static var count = 0;
var _name;
function Area( name ) { this._name = name; this.count++ }
function destroy() { this.count--; }
static function count() { return this.count; }
function name() { return this._name; }
function setName( name ) { this._name = name; }
}
In this example we define the "Area" class. When an instance of the class is constructed:
var area = new Area( "Berkshire" );
the given name is assigned to the object's _name variable and the class's static count is
incremented. The destroy() function is not called automatically; it must be called explicitly if
there is any clean-up to do. JavaScript does not have destructors.
All the class's variables and functions must be defined within the class definition.
Classes are all derived from Object. It is possible to derive a class from another class using the
extends keyword, for example:
class City extends Area {
var _population;
function City( name, population )
{
Area( name );
_population = population;
}
function population() { return _population; }
function setPopulation( population ) { _population = population; }
}
See also function.
const
const identifier = Value;
This keyword is used to define constant values. The identifier is created as a constant with the
given Value. The constant is global unless defined within the scope of a class or function.
Example:
const PI2 = Math.PI * 2;
const COPYRIGHT = "Copyright (c) 2006";
Attempts to assign to a constant cause the interpreter to issue an error message and stop.
function
function functionName( arguments )
{
Statements;
}
Functions may also be declared within the scope of a class definition. In this situation, the
functions become member functions of the class that contains them. See class.
399
Enfocus Switch 10