The Object class is the root of all the classes in ActionScript, all are an extension of the Object Class.
Object is a dynamic class, meaning it permits adding new properties and methods, it enables you to create your own properties and methods.
var objectName:Object = new Object();- "objectName" becomes an object to which you can define its own properties and methods.
var objectName:Object = {prop1:val1, prop2:val2, ...}- "prop1", "prop2" represents the properties of the "objectName" object, and "val1", "val2" are their values.
Once you have created an object with "new Object();", you can define its properties.
The most common form to define properties of an object is with the dot operator (.):
objectName.property_name = value;
objectName['property_name'] = value;
// Create an object "obj" var obj:Object = new Object(); // Define 2 properties, the first with dot operator, the second with [] obj.site = 'coursesweb.net'; obj['courses'] = 8; /* This object can also be created by using the syntax with curly brackets var obj:Object = {site:"coursesweb.net", courses:8}; Represents the same object */ // Initialize a "TextField" instance var txt:TextField = new TextField(); // Adds in "txt" instance a Text that contains the values of the "site" and "courses" properties // For "site" uses [], and for "courses" uses the dot (.) operator txt.text = obj["site"]+ ' - '+ obj.courses; // uses "addChild()" to add /display the text in the Flash presentation addChild(txt); // coursesweb.net - 8- If you add this script in a new Flash document, and press "Ctrl+Enter", the text "coursesweb.net - 8" will appear in the flash presentation".
Basically, methods are functions. The difference between functions and methods is that a function can be called directly by using its name, but the method is accessed through an object (or instance) in which it is defined.
Methods are defined with the dot notation (.).
To define a method, add the name of the method after the object's name, and for its value you must associate a function.
objectName.methodName = functionName;- "functionName" can be the name of a function you have created, or it can be a function created directly in the method definition, like in the following syntax:
objectName.methodName = function():DataType { ... function's code };The value obtained when you call a method is the value returned by the function associated to the respective method.
// Create the function that will be associated to a method function f_sum(a:Number, b:Number):String { var sum:Number = a+b; return 'The sum is: '+ sum; } // Define the object "obj2" with a property "id", and two methods "getSum" and "getId" var obj2:Object = new Object(); obj2.id = 'tutorials'; obj2.getSum = f_sum; obj2.getId = function():String { var idul = 'Id: '+ this.id; return idul; }; // Initialize a "TextField" instance in a "txt2" variable var txt2:TextField = new TextField(); // Adds in "txt2" instance a Text that contains the values returned by the "getSum()" and "getId()" // - "\n" adds new row txt2.text = obj2.getSum(7, 8)+ "\n"+ obj2.getId(); // uses "addChild()" to display the text in the Flash presentation addChild(txt2);- When the methods: "getSum()" and "getID()" are called, the program executes the code of the functions associated to them (each returns a string).
// defines "obj3" object, with the property "gen" and the method "Race()" var obj3:Object = new Object(); obj3.gen = 'Animals'; obj3.Race = function():String { return "Donkeys" }; // uses trace() to check their value trace(obj3.gen+ ' - '+ obj3.Race()); // Animals - Donkeys // changes the values of "gen" and "Race()" obj3.gen = 'Birds'; obj3.Race = function():String { return "Pigeons" }; // checks again their value trace(obj3.gen+ ' - '+ obj3.Race()); // Birds - Pigeons- You can see that the second trace() statement outputs a different result.
delete objectName.element;- "element" can be any property or method of the "objectName".
"element" in Class- "element" must be added between quotes and can be any property or method from "Class" ("Class" can also be the name of an object created with 'Object class').
// Define the "obj4" object with the property "gen" and the method "Race" var obj4:Object = new Object(); obj4.gen = 'Animals'; obj4.Race = function():String { return "Donkeys" }; // uses "in" to test their existance trace("gen" in obj4); // true if("Race" in obj4) { trace(obj4.Race()); // Donkeys } // Delete "gen" and "Race()" delete obj4.gen; delete obj4.Race; // checks again their existance trace("gen" in obj4); // false if("Race" in obj4) { trace(obj4.Race()); } // Try accessing them trace(obj4.gen+ ' - '+ obj4.Race()); // TypeError: Error #1006: Race is not a function ...- Notice the effect of "in" and "delete", and how the "if()" instruction is used.
<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