Home » ActionScript 3.0

Introduction to actionscript3.0 part3

24 November 2008 One Comment

In this part we are going to discuss about usage of Argument class, Rest keyword, difference between for..in and for each..in loop, void datatype, is as keyword.
Before going through this section its recommended to visit
Introduction to actionscript3.0 part1

Introduction to actionscript3.0 part2

Argument class:

An arguments object is used to access the arguments of the function and also usefull to get the reference to callee function.
Arguments inside the function can be accessed using the array notation, for example arguments[0] will refer the first argument in the function.

sayHello( "Hello !" );
function sayHello( _text:String )
{
  trace(arguments[0])
  sayBye( "Bye :) " );
}
function sayBye( _text:String )
{
  trace( arguments.callee )
}

Its recommended to use €¦(rest )keyword instead of arguments object.

Rest keyword:

Rest keyword in function specifies that it can accept n number of comma separated arguments. Later the arguments are available as array of values.

countlessArgs( "sdf", "wewe", "trer" );
function countlessArgs( ...rest )
{
  trace( rest.length );
  trace( rest );
  trace( rest[0] );
}

For..in

Used to enumerate the dynamic properties of an Object or Array.
Using for in we cannot enumerate the fixed properties of class.

The order of the elements may vary from the order in Object.

var obj:Object = {country:"India",states:28,unionTerritories:7};
for( var str in obj)
{
  trace(str +" : "+obj[str]);
}

For each..in

Used not only to iterate the XML Objects but also to iterate Object and Array.

We can iterate only through the dynamic properties of the Class again. To iterate through the properties of
user defined class we have to declare the properties of the method using the Dynamic keyword.

var obj:Object = {country:"India",currency:"Indian Rupee", capital:"New Delhi"};
for each( var str in obj)
{
  trace(str);
}

The differnce between the for..in and for each..in loop is

for..in loop iterates through the property names and for each..in loops through the values.

Undefined and NAN:

In Actionscript 2.0

Var _index:Number;
Trace(_index);
//outputs undefined

In ActionScript 3.0
Var _index:Number;
Trace(_index);
//outputs NaN

And if you declare a variable as type int, outputs 0 and for string its of type null

Void data type:

Any variable that are untyped can have any datatype value in it. Untyped variables lack type annotation are use (*) symbol for type annotation.
Var _untyped:*;
Only untyped values contains undefined, that is the default value.

var _data:*;
trace(_data)
var _str:String = "sara";
var _int:int = 10;
_data = _str;
trace(_data)
_data = _int
trace(_data);

Is as keyword

As keyword is to check first operant is member of the datatype specified in second operant.

var _int:int = 10;
trace(_int as int);
//outputs 10;
var _int:int = 10;
trace(_int as String)
//outputs null

Is keyword is to check whether an object is compatible with specified Class, Interface or Datatype.
And expression returns true if object is compatible with the class otherwise returns false.

var _sprite:Sprite = new Sprite();
trace(_sprite is Sprite);

Default value in argument

Now in actionscript3.0 we can make specify values for argument in function, it makes argument as an optional parameter.

Default values need to be specified at the end of the arguments. Arguments without default values are called required parameters.

trace(addNumbers( 1, 5 ));
//outputs 6
trace(addNumbers( 3 ));
//outputs 13
function addNumbers( a:Number, b:Number = 10):Number
{
  return (a+b)
}

Download Source Files

VN:F [1.7.7_1013]
Rating: 0.0/10 (0 votes cast)
VN:F [1.7.7_1013]
Rating: 0 (from 0 votes)
Translate this post
        
        
        
        
  

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.