Since others are in no hurry to come up with very good explanations, I will instead try to answer questions that you ask implicitly:
At this point, I think mySoundArray[0] will reference the same object as myAmbientSound
But myAmbiendSound does not reference anything. From your code:
var myAmbientSound:Sound;
The above simply creates a local variable of type Sound, a potential reference. A βfinger,β which may indicate something, in a simple environment. All local variables are references. And by the way, not all links are local variables, the properties of the object are also links, as well as Array and Vector elements. In any case, the expression above does not create an object of class Sound . It simply declares a local variable that can refer to an object of the Sound class. When declared, it has the special value undefined .
There is a difference between declaring a variable as described above and assigning it a value. The new operator creates an object and returns a reference to it. The assignment operator = leads to what is on the left side, everything on the right is rude.
One way to have a local variable above an object reference:
myAmbientSound = new Sound();
In your case, as I said, the variable myAmbientSound has the special value undefined , since you just declared it and it has not yet been assigned a value. In addition, as we see from the code, it does not refer to anything during the entire execution time of your code fragment.
Now, before I explain that you are inserting a string into your array:
var mySoundArray:Array = [ myAmbientSound, ...
you must remember that the elements of the array, as well as the properties of the object, are also references. mySoundArray[0] can refer to an object, as can mySoundArray[1] , etc.
The line of code above declares a new local variable and makes a reference to a new array object. Declaration and definition in one statement.
Now, since we have established that your local variable myAmbiendSound contains the special value undefined , your first element of the array ends with a reference to the same value - undefined , initially . In the loop later, you have the first element (or, if we want to be hypocritical, the element in the number that the i variable refers to) refers to the new Sound object. At this moment (and for the entire area of ββyour fragment, as we noticed), since myAmbiendSound is undefined , comparison of the two for equality does not work - they do not refer to the same object, nor the objects to which they refer are considered "equal "(how = works in AS3 - this is another topic) - the first is undefined , and the latter points to a Sound object.
In addition, a minor correction: "access" to the object does not result in an exception at runtime. An attempt to access a property (using the dotted notation syntax) of a link that does not point to an object - however, raises an exception at run time. As if you were writing trace(null.foo) - null not an object and certainly does not have a property named foo . The same goes for undefined .
By the way, null == undefined is true , but null === undefined is false . Just saying it. In rare cases (depending, of course) you will have to observe this detail.
Hope this clarifies the issue. I consider this an addition to all that was said earlier here.