(The contents of this topic is adapted from documentation provided by Trolltech.)
JavaScript identifiers match the regexp pattern [_A-Za-z][_A-Za-z0-9]*. Identifiers are used for variables, constants, class names, function names and labels. JavaScript reserves some words which are valid identifiers for its own use. See declarations for the complete list.
Variables are declared using the var keyword:
var a; // undefinedvar c = "foliage"; // the string "foliage"x = 1; // global variable
If a variable is assigned without being declared, it is automatically declared as a global variable. Using global variables can make your code difficult to debug and maintain and is not recommended.
Constants are declared using the "const" keyword:
const x = "Willow"; const y = 42;
Constants must be defined at the point of declaration, because they cannot be changed later. If an attempt is made to assign to a constant, the JavaScript interpreter will issue an error message and stop.
Constants are public globals if they are declared outside of any enclosing braces. When declared within the scope of some braces, (example: within an "if" statement) their scope is local to the enclosing block.
JavaScript is a fully object oriented language. Classes can be defined using the class keyword as shown in the example below.
class Circle { var m_x; var m_y; var m_r; function Circle( posx, posy, radius ) { m_x = posx; m_y = posy; m_r = radius; } function setX( posx ) { m_x = posy; } function setY( posy ) { m_y = posy; } function setR( radius ) { m_r = radius; } function x() { return m_x; } function y() { return m_y; } function r() { return m_r; } } class ColorCircle extends Circle { var m_rgb; function ColorCircle( posx, posy, radius, rgbcolor) { Circle( posx, posy, radius ); m_rgb = rgbcolor; } function setRgb( rgbcolor ) { m_rgb = rgbcolor; } function rgb() { return m_rgb; } }
A class's constructor is the function which has the same (case-sensitive) name as the class itself. The constructor should not contain an explicit return statement; it will return an object of its type automatically. JavaScript does not have a destructor function (a function that is called when the class is destroyed).
The class's member variables are declared with var, and its member functions with function.
The object instance itself is referred to using this operator. Inside a member function of a class, member variables and member functions can be accessed with an explicit "this" (example: this.x = posx;). This is not required, but can sometimes help to increase visibility.
JavaScript supports single inheritance and if a class inherits from another class, the superclass's constructor can be called with super().
When an object of a particular type is declared, the object itself becomes, in effect, a namespace. For example, in JavaScript there is a function called Math.sin(). If you wanted to have a sin() function in your own class that would not be a problem, because objects of your class would call the function using the object.function() syntax. The period is used to distinguish the namespace a particular identifier belongs to.
In most cases JavaScript is intelligent enough to work out the fully qualified name on its own. The only time that you need to qualify your names is when an unqualified name is ambiguous.
A property is an undeclared variable that can be written to and accessed if the class supports properties.
var obj = new Objectobject.myProperty = 100;
The class Object does not define the variable myProperty but since the class supports properties, we can define the variable with that name on the fly and use it later. Properties are associated with the object they are assigned to, so even though the object (obj in the above example) gets the property myProperty, it does not mean that other objects of type Object will have the property "myProperty", unless explicitly stated.
JavaScript supports the same commenting syntax as C++. One-line comments may appear on a line of their own or after the statements on a line. Multi-line comments may appear anywhere.
// A one-line comment. /* A multi-line comment. */