Since others have taken their time to come up with some very good explanations, I will instead try to address the questions you ask implicitly:
At this point, I'd think that mySoundArray[0]
would reference the same object as myAmbientSound
But myAmbiendSound
does not reference anything. From your code:
var myAmbientSound:Sound;
The above just creates a local variable of type Sound, a potential reference. A "finger" that can point to something, in laymans terms. All local variables are references. And by the way, not all references are local variables, object properties are references too, as are Array
and Vector
elements. Anyway, the expression above does not create an object of Sound
class. It merely declares a local variable that may reference an object of the Sound
class. Upon declaration it has a special value of undefined
.
There is a difference between declaring a variable like above and assigning it a value. Operator new
creates an object and returns a reference to it. The assignment operator, =
, results in whatever is on the left side reference whatever is on the right side, put roughly.
One way to have the local variable above reference an object would be:
myAmbientSound = new Sound();
In your case however, like I said, the myAmbientSound
variable has a special undefined
value instead, since you merely declared it, and it hasn't yet been assigned a value. Also, as we can see from the code, it does not reference anything during the entire runtime of your code snippet.
Now, before I explain what you end up putting into your array with the line:
var mySoundArray:Array = [ myAmbientSound, ...
, you have to remember that array elements, much like object properties, are references too - e.g. mySoundArray[0]
may reference an object, as may mySoundArray[1]
and so on.
The line of code above declares a new local variable and makes it reference a new array object. Declaration and definition in one single statement.
Now, since we have established that your local variable myAmbiendSound
holds the special undefined
value, your first array element ends up referencing that same value - undefined
, initially. In the loop later on you have the first element (or, if we are to be prudish, element at the number referenced by variable i
) reference a new Sound
object. At that point (and for the entire scope of your snippet, like we observed), since myAmbiendSound
is undefined
, comparing the two for equality will fail - they do not reference the same object, nor are the objects they reference considered "equal" (how =
works in AS3 is another topic) - the former is undefined
and the latter points to a Sound
object.
Also, a minor correction: "accessing" an object does not result in an run-time exception. Attempting to access a property (using dot notation syntax) of a reference that does not point to an object - does result in a run-time exception however. It's as if you'd write trace(null.foo)
- null
is not an object, and certainly has no property named foo
. Same goes for undefined
.
By the way, null == undefined
is true
, but null === undefined
is false
. Just saying. On a seldom occation (depending, of course), you will have to honor this detail.
I hope this clears matters up. I consider it an addition to all said earlier here.