JScript .NET, Part II: Major Features: Constant Variables and Objects - Doc JavaScript | WebReference

JScript .NET, Part II: Major Features: Constant Variables and Objects - Doc JavaScript


JScript .NET, Part II: Major Features

Constant Variables and Objects

The const declaration is very common in other programming languages. It allows you to declare a variable as non-changing. The values of variables declared as const remain constant throughout the life of the application. The advantage of using the const declaration is in making the variable's maintenance a lot easier. When you debug a program, it's very helpful to know which variables can change and which variables cannot change. The syntax of the const declaration is as follows:


const name1 [: type1] = value1 [, ... [, nameN
  [: typeN] = valueN]];

where:

name1, ..., nameN 

are the names of the constants being declared. They are, of course, required. Also:

type1, ..., typeN 

are the types of the constants being declared. As mentioned in Page 2, the data types are optional in JScript .NET. Finally:

value1, ..., valueN 

are the values assigned to the constants. Here are some examples:

const index = 5;
const president : String = "Abraham Lincoln";
const factor : int = 35, twoThirds : float = 2./3.;

The constant variables above are all simple, in the sense that the data is stored directly in the address pointed to by the variable, and cannot be changed. When the variable references a complex data type such as the Object or the Array, only the reference to the data remains constant. The content of the Object or the Array can be changed. It allows you to keep the address of an object or an array fixed, and it also keeps you from deleting the reference to them. You can freely change the content of the object or each one of the array elements, but you cannot change the length of the array or the structure of the object. The following example declares an array of 12 objects of the Object data type (ECMAScript Edition 3):

const arrayOfObjects : Object[] = new Object[12];

Anywhere in the application you can assign a value to an element of the array:

arrayOfObjects[7] = "contentOfEighthElement";

Next: How to enumerate variables


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: April 22, 2002
Revised: April 22, 2002

URL: http://www.webreference.com/js/column108/3.html