In a class in ActionScript you can also create static elements (variable, methods, constante). Static elements belong exclusively to a class, and can not be accessed through an instance of the class, but directly using the name of the class.
• To create static elements, you use the keyword static in the definition of the element.
Variables defined in a class are associated with properties that can be accessed through an instance of that class (if they have the "public" attribute), with this syntax: instance_name.property
attribute static var prop_name:DataType = value;Static properties belong to the class and can be accessed with this syntax:
ClassName.prop_nameHere's an example. We create a class (named "elStatic") with two public properties, the second property is defined as static. - If you do not know how to create a class in AS3 and use it in Flash, first study the lesson: OOP - Classes and objects - Create Class
package { // Define "elStatic" class public class elStatic { // Define properties public var prop1:String = 'coursesweb.net'; public static var prop2:String = 'ActionScript'; // Static variable } }- Open a new Flash document, in "Actions panel" add the following code, then save this document in the same folder as the file with this class, "elStatic.as".
// Create an instance of the "elStatic" class var obj:elStatic = new elStatic(); // access the class property trace(obj.prop1); // coursesweb.net // trace(obj.prop2); // Will generate an error // acces 'prop2' directly through the class name trace(elStatic.prop2); // ActionScript- Press "Ctrl+Enter to see the result in Output.
Constants are variables with a fixed value, they are defined with the instruction const, and their value can not be changed.
Static constants are created by adding the keyword static before keyword const.
- To access a static constant, use the expression:
ClassName.CONSTANT- Inside the class, static constants can be accessed directly with their name or with the syntax above.
package { // Defining "elStatic" class public class elStatic { // Defining the properties public var prop1:String = 'coursesweb.net'; public static var prop2:String = 'ActionScript'; // Static variable // Defining constants public const TUTORIALS:String = 'web development'; public static const SITE:String = 'coursesweb'; // Static constant } }- In the Flash document add the following code:
// Create instance of the class "elStatic" var obj2:elStatic = new elStatic(); // access the class constants trace(obj2.TUTORIALS); // web development // trace(obj2.SITE); // Returns an error // access 'SITE' using the class name trace(elStatic.SITE); // coursesweb- If you delete the backslashes (//) in front of the expression "trace(obj2.SITE);", it will generate an error. The "SITE" constant, having the "static" attribute, is not recognized in the instance of the class ("obj2"), but can be accessed through the class name (elStatic.SITE).
To create a static method, add the keyword static before the keyword function, in the method definition.
attribute static function methodName() { // code to execute }
ClassName.methodName(parameters)- In the body of a static method you can only use other static elements (properties, constants, methods). The keyword "this" can not be used.
package { // Defining the "elStatic" class public class elStatic { // Defining properties public var prop1:String = 'coursesweb.net'; public static var prop2:String = 'ActionScript'; // Static variable // Defining constants public const TUTORIALS:String = 'web'; public static const SITE:String = 'coursesweb'; // Static constant // Defining a static method public static function Courses(param:String):String { var re:String; // The variable that will be returned // define "switch" instruction to set different posible values for "re" switch(param) { case 'web': re = elStatic.prop2; // "re" receives the value of the static property "prop2" break; case 'coursesweb': re = SITE; // "re" receives the value of the static constant "SITE" ( or elStatic.SITE; ) break; default: re = 'Default value'; } return re; // Returns "re" value } } }- Because the "Courses" method is declared as static, can use only variables and constants that are also static. If, for example, you add in it's body "re = this.prop1;" or "re = TUTORIALS;" it will generate an error.
var obj3:elStatic = new elStatic(); trace(elStatic.Courses(obj3.TUTORIALS)); // ActionScript // trace(obj3.Courses(elStatic.SITE)); // generate an error // Call the static method, uses a static constant for argument trace(elStatic.Courses(elStatic.SITE)); // coursesweb- if you delete the backslashes (//) in front of the expression "trace(obj3.Courses(elStatic.SITE));", it will generate an error. The method "Courses()" having the "static" attribute is not recognized in the instance of the class ("obj3"), but can be accessed with the class name (elStatic.Courses('argument')).
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net