Since ActionScript 3.0 is based on ECMAscript, it shares some similarities with javascript. One of the similarities that I played with is creating objects from functions.
In javascript to create an object
var student = new Student( 33 ); document.write( student.age ); function Student( age ){ this.age = age; }
In actionscript 3.0, Objects are usually created through a class, but objects can be created, as in javascript, through constructor functions.
package{ import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var student = new Student( 33 ); trace( student.age ); } } } function Student( age ) { this.age = age; }
However, I get a compilation error with the above code
Loading configuration file C: \ Program Files \ Adobe \ Flex Builder 3 \ sdks \ 3.1.0 \ frameworks \ flex-config.xml
C: \ Documents and Settings \ mallen \ Desktop \ as3 \ Main.as (5): col: 23 Error: Incorrect number of arguments. Expected 0
var student = new Student (33);
^
I was wondering why this is? To make things even weirder, the following code really works
package{ import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ Student( 33 ); var student = new Student(); trace(student.age);
Output for this code
33
undefined
undefined
source share