|
|
Functions
Functions (also known as a procedure, method, or subroutine) allows any object to
perform specific tasks under different conditions with the same block of code.
They are usually designed to be called from many different places, but can also
be flagged to be used only by the object or class to which it belongs (known as
Private functions). Each function has a name, and is given any number of values
to work with (often known as parameters or arguments). Functions typically
only use properties of the object to which they belong in addition to the arguments
supplied when they are called.
Example:
var Car = new Object();
Car.Speed = 0; // In Miles Per Hour
Car.FuelLevel = 15.0; // In Gallons
Car.KeyPosition = 0; // 0 is Off, 1 is Power, 2 is Start Engine
Car.BatteryCharge = 1.0; // 100%
Car.TurnKey = new Function()
{
switch(KeyPosition)
{
case 0:
if(BatteryCharge
> 0.1)
KeyPosition++;
break;
case 1:
if(FuelLevel
> 0.1)
KeyPosition++;
case 2:
trace("Car
already on.")
}
Car.AdjustAcceleration = new Function(petal_value)
{
if(KeyPosition == 2) // Double equals must be used for
comparisons
Car.Speed += petal_value;
} |
|
|
|
Previous PageSwitch Statement,
Next Page
Classes. |
|