Quantumwave Interactive Inc.
  Interactive media development . Programming . Design . Consulting

Demonstration of static methods and properties in ActionScript

Static (class) methods and properties vs. instance methods and properties:

Static or class methods and properties belong to the class and not the instances created from the class. There is only one copy of the static methods or properties in the class hierarchy. On the other hand, there are multiple copies of the instance properties in each instance, and the instance methods are shared among instances created from the class through the prototype chain.


TheCoolObject = function(coolFactor) {
   // instance variable
   this.coolFactor = coolFactor;
}

//------------------------------------------------------------------------------------

// instance method
TheCoolObject.prototype.getCoolFactor = function() {
   return this.coolFactor;
}

//------------------------------------------------------------------------------------

// static variable
TheCoolObject.maxCoolFactor = 100;

//------------------------------------------------------------------------------------

// static method
TheCoolObject.theCoolerObject = function(a, b) {
   if ((a.coolFactor > this.maxCoolFactor) || (b.coolFactor > this.maxCoolFactor)) {
      trace("Someone's cheating...");
      return null
   } else {
      return (Math.max(a.coolFactor, b.coolFactor)==a.coolFactor) ? a : b;
   }
}
Here's another example in Flash MX:

// Astronaut class
Astronaut = function(w) {
   // how heavy this astronaut is
   this.weight = w;
   
   // each astronaut requires one oxygen tank
   this.oxygenReserve--;
};

// The initial number of oxygen tanks reserved for astronauts (a static/class property)
Astronaut.oxygenReserve = 10;

// This next line does not define a static/class property, it is an instance property
// Astronaut.prototype.oxygenReserve = 0;

// A static/class method (compares two instances/astronauts)
Astronaut.heavierAstronaut = function(a, b) {
   return (a.weight > b.weight) ? a : b;
};

//------------------------------------------------------------------------------------

// Store the prototype object for quick access (and less typing)
var ap = Astronaut.prototype;

// An instance method to get the oxygenReserve
ap.getOxygenReserve = function() {
   return this.constructor.oxygenReserve;
};

// An instance method to set the oxygenReserve
ap.setOxygenReserve = function(o) {
   this.constructor.oxygenReserve = o;
};

ap.addProperty("oxygenReserve", ap.getOxygenReserve, ap.setOxygenReserve);

// Delete the temp variable
delete ap;

//------------------------------------------------------------------------------------

trace("Astronaut.oxygenReserve: "+ Astronaut.oxygenReserve);

// Tests
var a1 = new Astronaut(220);
var a2 = new Astronaut(175);

trace("Astronaut.oxygenReserve: "+ Astronaut.oxygenReserve);

// Each astronaut checks the number of tanks left
trace("a1.oxygenReserve: "+ a1.oxygenReserve);
trace("a2.oxygenReserve: "+ a2.oxygenReserve);

//------------------------------------------------------------------------------------
// Astronaut 1 needs more oxygen a1.oxygenReserve--; // Now this static/class property is reflected in both astronauts trace("a1.oxygenReserve: "+ a1.oxygenReserve); trace("a2.oxygenReserve: "+ a2.oxygenReserve); //------------------------------------------------------------------------------------ // Astronaut 2 refills three tanks of oxygen a2.oxygenReserve += 3; // Now this static/class property is reflected in both astronauts trace("a1.oxygenReserve: "+ a1.oxygenReserve); trace("a2.oxygenReserve: "+ a2.oxygenReserve); //------------------------------------------------------------------------------------ // Is astronaut 1 the heavier astronaut? trace(a1 == Astronaut.heavierAstronaut(a1, a2));
Download source files: staticObj.zip / staticObj.sit (Flash 5) or staticDemoMX.zip (Flash MX)

 
Notes:

ActionScript doesn't have real static properties or methods as in Java, the code above is only a way to achieve similar results.

Last updated on: May 3, 2002 - Copyright © 2001-2002 Dave Yang / Quantumwave Interactive Inc.

home | about | news | flash | director | shockwave | download | dave | résumé | contact

Copyright © 1995-2009 Quantumwave Interactive Inc.