I’ve always found this interesting, and haven’t managed to fully understand it yet. Take this class:
package
{
public class SomeClass
{
private var _myvar:String = "hello";
public static function sayHello():void
{
trace(_myvar);
}
}
}
As we know, this will throw an error unless I declare _myvar as private static var _myvar
1120: Access of undefined property _myvar.
I don’t completely understand why _myvar isn’t in scope unless it’s static. Can anyone provide a easy to understand explanation of this?
As
_myvaris not static it’s a property that is attached to an instance ofSomeClass, i.e. it has only meaning each time you create a newSomeClass.In contrary a
staticproperty is attached to the Class object, it exists only once and is not dependent of each instance of SomeClass created.So when you try to reach
_myvarfrom yourstatic function sayHelloit’s the same as callingthis._myvarbut you have only one static property and can have multiple instance created over the timelife of your application, what should be the value ofthisin that case ?