July 2000 Draft
JavaScript 2.0
Core Language
Variables
previousupnext

Monday, May 8, 2000

Variable Definitions

VariableDefinition  VariableDefinitionKind VariableBindingListallowIn
VariableDefinitionKind 
   var
|  const
VariableBindingList 
   VariableBinding
|  VariableBindingList , VariableBinding
VariableBinding 
   TypedVariable
|  TypedVariable = AssignmentExpression
TypedVariable 
   QualifiedIdentifier
|  QualifiedIdentifier : TypeExpression

A variable defined with var can be modified, while one defined with const is read-only. Identifier is the name of the variable and TypeExpression is its type. Identifier can be any non-reserved identifier. TypeExpression is evaluated at the time the variable definition is evaluated and should evaluate to a type t.

If provided, AssignmentExpression gives the variable's initial value v. If AssignmentExpression is not provided in a var definition, then undefined is assumed. AssignmentExpression is evaluated just after the TypeExpression is evaluated. The value v is then coerced to the variable's type t and stored in the variable. If the variable is defined using var, any values subsequently assigned to the variable are also coerced to type t at the time of each such assignment. If the variable is an instance variable of a class or interface, then the value v is saved and subsequently used to initialize a slot in each instance constructed.

Multiple variables separated by commas can be defined in the same VariableDefinition. The values of earlier variables are available in the TypeExpressions and AssignmentExpressions of later variables. The static extent of a variable begins immediately after its AssignmentExpression.

If omitted, TypeExpression defaults to type any. Thus, the definition

var a, b=3, c:Integer=7, d, e:Type=Boolean, f:Number, g:e, h:int;

is equivalent to:

var a:any=undefined;
var b:any=3;
var c:Integer=7;
var d:Integer=undefined;  // coerced to NaN
var e:Type=Boolean;
var f:Number=undefined;   // coerced to NaN
var g:Boolean=undefined;  // coerced to false
var h:int=undefined;      // coerced to int(0)

Constant Definitions

const means that assignments to Identifier are not allowed (unless a setter for Identifier is also defined). Multiple const declarations of the same variable are allowed, but only one of them can be a definition (i.e. have an AssignmentExpression). If multiple TypeExpressions are provided, they must evaluate to the same type. If a TypeExpression is not provided by the time the AssignmentExpression is evaluated, the type defaults to any. Any attempt to read the constant prior to writing its value will result in an error. For example:

const c:Integer;

function f(x) {return x+c}

f(3);  // error: c's value is not defined
const c = 5;
f(3);  // returns 8
const c = 5; // error: redefining c

Just like any other definition, a constant may be rebound after leaving its scope. For example, the following is legal; j is local to the block, so a new j binding is created each time through the loop:

var k = 0;
for (var i = 0; i < 10; i++) {
  local const j = i;
  k += j;
}

Getters and Setters

A definition var x:t = v internally creates a hidden variable and defines a getter and a setter to access that variable:

A definition const x:t = v internally creates a hidden variable and defines a getter to access that variable:

This relationship between a variable and its getter and setter is normally transparent but can be exploited occasionally. For instance, a variable can be declared that is private for writing but public for reading:

private var name:String;
public export get name;

A subclass may override a variable's getter or setter. To do this the original variable has to be declared non-final because variables are final by default:

class C {
  virtual var x:Integer;
  var y:Integer;
}

class D extends C {
  override function set x(a:Integer):Integer {return y = a*2}
}

var c = new C;
c.x = 5;
c.x;       // Returns 5
c.y;       // Returns NaN (the default value for an Integer variable)
var d = new D;
d.x = 5;
d.x;       // Returns NaN
d.y;       // Returns 10

Waldemar Horwat
Last modified Monday, May 8, 2000
previousupnext