Chris Velevitch wrote:
I think the confusion occurs because you seem to be mixing up the
distinction between declaration and reference.

Another distinction, which isn't very clear in ActionScript, is the distinction between declaration and definition. The declaration is where you tell the compiler what is supposed to exist. The definition is where you create the things that actually exist. The definition needs to match the declaration.

In ActionScript these generally happen at the same time, e.g.:

   var x : Number;

That means:

   1. Declare that a variable called "x" exists and has the type "Number"
2. Define a variable called "x" and allocate the memory for it. Make it a Number.

In ActionScript, you can't have a declaration without a definition. However, you can have a definition without a declaration. E.g.:

   this.x = 10;

That's a definition. "x" is a variable of type Number containing the number 10.

If you happen to have a declaration elsewhere that says, for instance, that x should be a String, then this will cause a compiler error because the definition doesn't match the declaration. However, if there is no declaration and you are not working with a non-dynamic class, ActionScript just allows this to go as a "dynamic" variable: one whose type is not known at compile time because it is defined at runtime without a formal declaration.

Variables are always declared with "var" because that ensures they are being created in the local scope. You can't go declaring things in a different scope, because aside from anything else, that would break the OOP black-box model. It would be like me coming into your house and rearranging your furniture while you were out. It doesn't work like that, because the compiler needs to rely on a single, authoritative declaration.

- Robert
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to