Skip to content

Class Variable Weirdness

Categories: Flash

Table of Contents

This is a weird bug/feature I found in Flash a week or two ago. I like to declare my variables in classes like this:

class electric {
	var voltage = 100;
	
	function electric() {}
}

Well, I was working on a project a few weeks ago and I had a class that looked something like this:

class data {
	var info = [];
	
	function data() {}

}

Throughout the SWF I code like this:

dataOb.info.push("info 1", "info2");
dataOb2.info.push("info 3", "info 4");

When I traced dataOb, I got this output:

info 1, info2, info 3, info 4

But I only added “info 1” and “info 2” to the dataOb.info array? Why would “info 3” and “info 4” be in their too? It seems as though if you declare a variable as an array in the variable declaration and dont re-initalizie it in the constructor (IE, info = []) then the array will be shared across instances of the class. This effect doesn’t seem to happen with number declaration like the one in the example electric class I posted. Here’s an example showing what I just explained.

Keep in Touch

Subscribe to my email list to keep in touch. I’ll send you new blog posts and other thoughts.