It is not a bug, it is the way ActionScript & JavaScript... and any ECMA 
Script language works.

If others have read this thread, or this phat book:
http://www.debreuil.com/docs/ch01_Intro.htm

...or read Ralf's blog:
http://www.helpqlodhelp.com/blog/archives/000064.html

...or used JavaScript OOP extensively, then they should already know.

AS3's different.  It exists on the traits, not the prototype.  Prototype is 
now read-only, and ... well, not really used anymore.  This is good and bad.

The Array example, however, I know for a fact is different.  It treats it 
like it should; if any instance adds something to the array, it immediately 
get it's own copy... although, I don't think prototype varialbes are even 
accessible.  I'll have to defer that one to someone more knowledgeable about 
AS3's inner workings.

Don't initialize arrays in your class, and initialize in your constructor or 
init functions, and you'll never have problems.



----- Original Message ----- 
From: "Judah Frangipane" <[EMAIL PROTECTED]>
To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
Sent: Thursday, December 29, 2005 5:56 PM
Subject: Re: [Flashcoders] Flash Class Array bug - Please verify


The thing that bothers me is that no one seems to care if this is a bug
or not. I didnt realize that initializing an Array in the declaration
would make a static like variable. Its not really static and it doesn't
work the same for primitives. I spent almost 2 days going over code
trying to solve this. I had to post it here and show it to another
developer to pinpoint the problem. So do I report this as a bug? Do I
announce it for others to take note of? Does it exist in AS3? Do I live
with it until I move to AS3? Yeah, I don't know...

elibol wrote:

>The only response that accually addresses the problem is JesterXL's, he
>seems to have hit the nail right on the head. It seems the only
>differentiating aspect of static and prototype members is the way they 
>treat
>by refrence types.
>
>On 12/29/05, JesterXL <[EMAIL PROTECTED]> wrote:
>
>
>>Sorry, missed 1 of the traces:
>>
>>// notice the array is on the prototype,
>>// thus all class instances point to it
>>foo.prototype_array.push("foo_d");
>>trace(foo.prototype_array);
>>trace(bar.prototype_array);
>>
>>should be:
>>
>>// notice the array is on the prototype,
>>// thus all class instances point to it
>>foo.prototype_array.push("foo_d");
>>trace(foo.prototype_array); // b, foo_d
>>trace(bar.prototype_array); // b, foo_d
>>
>>----- Original Message -----
>>From: "JesterXL" <[EMAIL PROTECTED]>
>>To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
>>Sent: Thursday, December 29, 2005 3:39 PM
>>Subject: Re: [Flashcoders] Flash Class Array bug - Please verify
>>
>>
>>Nope, they are prototype scoped arrays, not static.
>>
>>AS1:
>>function MyClass(){}
>>
>>AS2:
>>class MyClass{}
>>
>>AS1 - static:
>>MyClass.my_array = [];
>>
>>AS2 - static:
>>class MyClass
>>{
>>    static var my_array:Array = [];
>>}
>>
>>AS1 - prototype:
>>MyClass.protoytpe.my_array = [];
>>
>>AS2 - prototype:
>>Same as above, or:
>>class MyClass
>>{
>>    var my_array:Array = [];
>>}
>>
>>Prototype values, even in the class intstance, point to the prototype
>>value
>>when read, but as soon as an instance modifies this value, it makes it's
>>own
>>copy if it's a primitive (String, Number).  If it's an Object, it still
>>points to the prototype reference.  Example:
>>
>>// The Class
>>class MyClass
>>{
>>static var static_array:Array = ["a"];
>>var prototype_array:Array = ["b"];
>>var instance_array:Array;
>>
>>var prototype_name:String = "MyClassName";
>>
>>function MyClass()
>>{
>>  instance_array = ["c"];
>>}
>>}
>>
>>// And here, the usages:
>>
>>import MyClass;
>>
>>trace(MyClass.static_array); // a
>>trace(MyClass.prototype.prototype_array); // b
>>trace(MyClass.prototype.instance_array); // undefined
>>
>>var foo:MyClass = new MyClass();
>>trace(foo.prototype_array); // b
>>trace(foo.instance_array); // c
>>
>>var bar:MyClass = new MyClass();
>>trace(bar.prototype_array); // b
>>trace(bar.instance_array); // c
>>
>>// notice the array is on the prototype,
>>// thus all class instances point to it
>>foo.prototype_array.push("foo_d");
>>trace(foo.prototype_array);
>>trace(bar.prototype_array);
>>
>>// however, for primitives, instances
>>// will get their own copy once they
>>// change it
>>
>>// before
>>trace(foo.prototype_name); // MyClassName
>>trace(bar.prototype_name); // MyClassName
>>foo.prototype_name = "MegaFoo";
>>// after
>>trace(foo.prototype_name); // MegaFoo
>>trace(bar.prototype_name); // MyClassName
>>
>>
>>
>>----- Original Message -----
>>From: "Judah Frangipane" <[EMAIL PROTECTED]>
>>To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
>>Sent: Thursday, December 29, 2005 3:30 PM
>>Subject: [Flashcoders] Flash Class Array bug - Please verify
>>
>>
>>It seems like Flash is creating static members out of public arrays that
>>are initialized in the class declaration. Can someone confirm this? Is
>>this not legal, is it implied behavior or a bug?
>>
>>// create a class called myClass
>>class myClass {
>>    // initialize the array in the declarations
>>    var myArray:Array = new Array();
>>
>>    function myClass {
>>       trace("myArray.length="+myArray.length)
>>    }
>>
>>    function addToArray() {
>>       myArray.push(10)
>>       myArray.push(20)
>>    }
>>
>>}
>>
>>
>>// on the actions frame
>>import myClass
>>
>>var a = new myClass();
>>a.addToArray();
>>a.addToArray();
>>a.addToArray();
>>var b = new myClass();
>>
>>// trace outputs
>>myArray.length=0
>>myArray.length=3
>>
>>Judah
>>_______________________________________________
>>Flashcoders mailing list
>>Flashcoders@chattyfig.figleaf.com
>>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>>_______________________________________________
>>Flashcoders mailing list
>>Flashcoders@chattyfig.figleaf.com
>>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>>_______________________________________________
>>Flashcoders mailing list
>>Flashcoders@chattyfig.figleaf.com
>>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>>
>>
>_______________________________________________
>Flashcoders mailing list
>Flashcoders@chattyfig.figleaf.com
>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
>
>
>
>

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders 

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to