views:

1262

answers:

2

Since actionscript 3.0 is based on ECMAscript it shares some similarities with javascript. One such similarity that I have been playing around 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 class, but Objects may be created, like in javascript, through constructer 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 compile 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 does work

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);
}

The output for this code is

33
undefined
undefined
+1  A: 

You need to declare Student in it's own class within the file. That way Student is accessible anywhere within 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;
   }
}
James Hay
No, that makes a class student, I think you misunderstood. Functions can exist outside of classes.
ForYourOwnGood
+5  A: 

Syntactically, this is one area (among many) where the two diverge. ;)

You can create an object using a 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 a slightly different syntax. The Function class is also dynamic, which means you can graft methods onto its instances at runtime (as I do above with sayHi()), much as you might use JavaScript's "prototype" property.

I'm actually not sure what kinds of annoyances, naming collisions, oddities, etc., you might run into with this approach, since I haven't yet dug deeply into into the docs on it -- but it does work!

Christian Nunciato
Where two what diverge?
bzlm
JavaScript and ActionScript. (Looks like I left out a "the" -- thanks for the head's up.)
Christian Nunciato