Classes, functions, variables and constants are declared with class, function, var and const respectively.
JavaScript reserves some words which are valid identifiers for its own use:
Currently used for declarations: class, function, var and const.
Reserved for future use: boolean, byte, char, debugger, double, enum, export, float, goto, implements, import, int, interface, long, native, short, synchronized, throws, transient and volatile.
It is unadvisable to use any of these words as identifiers.
class ClassName { static var ClassVariable; var MemberVariable; static function ClassFunction { Statements; } function ClassName() { Statements; } // constructor function MemberFunction() { Statements; } }
This keyword is used to define new classes. After the keyword class comes the ClassName, then optionally, the keyword extends followed by a class name from which this class derives, then an opening brace. Class variables are declared with static var (ClassVariable). Only one instance of these variables exists per class. Member variables (MemberVariable) are declared with var; each instance (object) of the class has its own copy of each member variable. Functions declared with the keywords static function are class functions (ClassFunction); these functions are called using the name of the class rather than from an object. In the standard library, the Math functions are all static, for example Math.sin(). Member functions are called by objects and are declared with function. The constructor is the member function with the same name as the class; constructors must not contain an explicit return statement or have an explicit return type, since JavaScript handles these automatically.
A class that only contains static const, var and function definitions does not need a constructor.
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 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 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.
var variableName; var anotherVariableName = InitialValue;
This keyword is used to declare and optionally initialize variables. If just the variableName is given, the variable is created, but it has no value, that is, its value is undefined. If an InitialValue is given, the variable is created and assigned this InitialValue. Variables declared within functions are local to the function in which they are declared. Variables declared outside of functions and classes are global.
Example:
var i; var count = 22; var str = "string";