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

Reply via email to