MyClass.prototype.myArray = new Array();

That is indeed correct. It should be in the archives somewhere (if they go back that long).

regards,
Muzak

----- Original Message ----- From: "Andy Herrman" <[EMAIL PROTECTED]>
To: "Flash Coders List" <[email protected]>
Sent: Wednesday, March 12, 2008 9:02 PM
Subject: Re: [Flashcoders] clean scripting


Here's my understanding of the reason behind this:

AS2 is basically just syntactic sugar over AS1, and gets compiled down
to the same thing.

When defining a class you're actually defining things on the
prototype, so doing this:
--------------
class MyClass {
 public var myArray:Array;

 public function MyClass() {
   myArray = [];
 }

 public function push(o:Object):Void {
   myArray.push(o);
 }
}
--------------

would be the same as:

--------------
MyClass = function() {
 this.myArray = [];
}

MyClass.prototype.push = function(o) {
 this.myArray.push(o);
}
--------------

If you set the value of the member variable at declaration:
--------------
 public var myArray:Array = new Array();
--------------

It turns into this:
--------------
MyClass.prototype.myArray = new Array();
--------------

Since you have just assigned an array instance to the prototype of the
class, that gets shared between all instances of the class (basically,
the value you set there is the initial value given to the myArray
member of the class on instantiation).

Well, assuming my understanding is correct. :)

 -Andy

On Wed, Mar 12, 2008 at 2:02 PM, Dave Mennenoh
<[EMAIL PROTECTED]> wrote:
>>So yea...WTF? I can't believe after my years of AS2 coding that it would
 >>have taken me this long to notice.

 I think I muttered those exact words. Bizzare behavior. Though I don't think
 I have ever run into it. Someone taught me long ago not to initialize class
 variables in their definitions. So I just never have done that. Really good
 to know though.

 Dave -
 Head Developer

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to