简体   繁体   中英

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 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

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!

You need to declare Student in its 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;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM