You control access to a member variable when using the get / set functions. For example, if you want the variable to be read-only from the outside, but edited from an instance of the class, you make the get function so that it can be read from the outside, but NOT to create a set function. This is different from using private const, because it must be declared immediately and can never be changed anywhere.
Similarly, using these functions allows you to create side effects to set the property. For instance:
public function set foo(value:*):void{ _foo = value; this.dispatchEvent(new Event("fooSet"));
EDIT: because you clarified the question to be more specific, here's an update of my own.
Usually you did not. If you try to protect this variable, you will not offer it directly. This violates the "law of Demeter." For your specific example with an array, you can do something like this:
private var _someArray = [true,false]; function get someArray():Array{ return _someArray.slice();
As another example, using a theoretical complex object ...
private var _someObject:SomeObject; function get someObject():SomeObject{ return _someObject; // "Wrong." It breaks the law of demeter. } ////// instead, you would do this..... function get getSomeObjectsInt():int{ return _someObject.foo; // where .foo is an int } ////// or this.... function doStuffWithFooObject():Boolean{ return _someObject.doSomething(); // where doSomething returns a boolean; } ///// or this..... function performActionOnData(pData:String):String{ return _someObject.someActionWithString(pData); }
This last one is interesting because you don’t have to tell the world that you are using SomeObject to do this work ... you just advertise that you can do it yourself.
source share