Actionscript Course

Nested Objects

Nested objects are objects created inside another object. So, the property of an object (it's value) can be an Array or even another object.
Let's see an example:.

// Create a main object "Courses", with 2 properties:"type" and "site", and a method "getCourse()"
// The first property, "type", will have as value an Array with 2 elements
// The second property "site", will have as value another object, which will have a property "url"
// The method "getCourse()" will use an element from the array in "type" property,
// and the property of the nested object

var Courses = new Object();
 Courses.type = ['lessons', 'tutorials'];         // property with an Array value
 Courses.site = new Object();              // property which contains a nested object
   Courses.site.url = 'coursesweb.net';         // property of the nested object
 Courses.getCourse = function(nr:int) {           // Method of the main object
   var sir1 = this.tip[nr];               // uses a value from the "type" property
   var sir2 = this.site.url;           // uses the value of the "url" property (of the nested object)
   return sir1+ ' - '+ sir2
 };

// Displays in Output the result returned by calling the method "getCourse()"
trace(Courses.getCourse(1));           // tutorials - coursesweb.net
- The property "site" is defined as a sub-object in the main object (Courses);
- Notice the expression used to call the property of the nested object "this.site.url", it accesses the "url" property which is defined in the "site" (the sub-object). "this" represents the current object in which the expression is added.
- The expression "Courses.getCourse(1)" calls the "getCourse()" method and passes it the value of 1. The method uses this argument to get an element of the array in the "type" property, "this.tip[nr]".

Traverse objects

You can traverse the elements of an object the same way as when traversing an associative Array, with the instruction "for..in" or "for each..in".
Study the following example, and the comments in code.

Example with for..in

// define aunction to traverse objects
// Takes 2 arguments: "obj" for the object which will be traversed, and "val" for the value that must be searched in it
function parsObj(obj:Object, val:*):String
{
  // define a variable to store the value returned by this function
  var re:String = 'no prop';

  // defina a "for..in" loop to traverse "obj"
  for(var key:String in obj)
  {
    // If the value of the current element is the same as the value of "val"
    // adds in "re" the name of that property and stops the loop
    if(obj[key]==val)
    {
      re = key;
      break;
    }
  }

  return re;
}

// Define object with 2 properties ("courses", "tutorials") and a method "Sum()"
var anObject:Object = new Object();
 anObject.course = 'Flash';
 anObject.tutorial = 'AS3';
 anObject.Sum = function(a:Number, b:Number):Number {
    return a+b;
  }

// Call the function "parsObj()", passing the object and value 'AS3'
trace(parsObj(anObject, 'AS3'));        // tutorial
- The "for(var key:String in obj)" loop stores in "key" the current property of the "obj", then, the "if(obj[key]==val)" checks if the value of this property is the same as the value of "val".
- The "re" variable is defined with an initial value, to be returned in case the "if()" instruction doesn't find a value to match the "val". The code above will display in output the string "tutorial".

Example with "for each..in"

// Define object with 2 properties ("courses", "tutorials") and a method "Sum()"
var anObject:Object = new Object();
 anObject.course = 'Flash';
 anObject.tutorial = 'ActionScript 3';
 anObject.Sum = function(a:Number, b:Number):Number {
    return a+b;
  }

// defina a "for each..in" loop to traverse "anObject"
// Displays in Output the value of each element
for each(var elm:* in anObject)
{
  trace(elm);
}
This example will show in Output:
function Function() {}
ActionScript 3
Flash

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Nested Objects - Traverse object

Last accessed pages

  1. Shape Tween - Flash Animation (6149)
  2. The Mastery of Love (7440)
  3. Get Mime Type of file or string content in PHP (6230)
  4. Countdown Timer with starting time added into a form (11533)
  5. Disable button and Enable it after specified time (17533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)