Bagikan melalui


var Statement

Declares a variable.

// Syntax for declaring a variable of global scope or function scope.
var name1 [: type1] [= value1] [, ... [, nameN [: typeN] [= valueN] ]]

// Syntax for declaring a variable field within a class.
 [attributes] [modifiers] var name1 [: type1] [= value1] [, ... [, nameN [: typeN] [= valueN].]]

Arguments

  • attributes
    Optional. Attributes that control the visibility and behavior of the field.

  • modifiers
    Optional. Modifiers that control the visibility and behavior of the field.

  • name1, ..., nameN
    Required. The names of the variables being declared.

  • type1, ..., typeN
    Optional. The types of the variables being declared.

  • value1, ..., valueN
    Optional. The initial value assigned to the variable.

Remarks

Use the var statement to declare variables. A variable may be bound to a specific data type to help provide type safety. These variables may be assigned values when they are declared, and these values may be changed later in the script. Variables that are not explicitly initialized are assigned the default value of undefined (coerced to the type of the variable if necessary).

A variable field in a class is similar to a global or function variable, except that it is scoped to the class and it can have various attributes governing its visibility and usage.

Example

The following example illustrates some uses of the var statement.

class Simple {
   // A field declaration of the private Object myField.
   private var myField : Object;
   // Define sharedField to be a static, public field.
   // Only one copy exists, and is shared by all instances of the class.
   static public var sharedField : int = 42;
}
var index;
var name : String = "Thomas Jefferson";
var answer : int = 42, counter, numpages = 10;
var simpleInst : Simple = new Simple;

Requirements

Version 1

See Also

Reference

const Statement

function Statement

new Operator

Concepts

Scope of Variables and Constants

Type Annotation

Other Resources

Modifiers