Using a function to create an object in ActionScript 3.0

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); /* When I add the two lines below, the code wont compile? */ //var student2 = new Student( 33 ); //trace(student2.age); } } } function Student( age ){ this.age = age; trace(age); } 

Output for this code

  33
 undefined
 undefined
+4
source share
2 answers

Syntactically, this is one of the areas (among many) where they diverge .;)

You can create an object using the function:

 private var studentName:String = "Joe"; private function init():void { var s = new Student("Chris"); trace(s.studentName); trace(this.studentName); trace(typeof s); trace(typeof Student); s.sayHi(); trace("Hello, " + s.studentName + ". I'm " + studentName + "."); } var Student:Function = function(studentName:String):void { this.studentName = studentName; this.sayHi = function():void { trace("Hi! I'm " + this.studentName + "."); }; }; // Chris // Joe // object // function // Hi! I'm Chris. // Hello, Chris. I'm Joe. 

... just with slightly different syntax. The Function class is also dynamic, which means you can embed methods in your instances at runtime (as I said with sayHi () above), since you can use the JavaScript "prototype" property.

In fact, I'm not sure what types of annoyance, naming conflicts, oddities, etc. you may come across this approach, since I have not yet deeply delved into the documents on it, but it works

+6
source

You need to declare Student in your class inside the file. Thus, the Student is available anywhere in Main.

 package{ import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var student = new Student( 33 ); trace( student.age ); } } } class Student { public var age : uint public function Student( age : uint ) { this.age = age; } } 
+1
source

All Articles