A class's members consist of properties, or variable declarations, and methods, or function declarations, as discussed in Principles of object-oriented programming.
Any variable declared within a class definition, but outside of a member function of that class, is a property of the class. The following code adds to the Plant class a property named leafType
(a string) and a method named getLeafType
, which returns the value in the leafType
property.
class Plant { // Property: leafType var leafType:String; // Method: getLeafType function getLeafType:String() { return leafType; } }
In this case, because the leafType
property was declared outside of a class method, it is considered a global variable. Any other method within the class can access its value.External class files, in turn, can only contain variable and function declarations. For example, any code defined outside the body of the class will generate an error, as shown below.
// Inside MyClass.as: class MyClass { } // Compiler error! Variable declared outside class body error
var foo = "bar";