Reputation: 17237
since the MXMLC compiler doesn't support mixed access modifiers for setters/getters, i would like to know whether a public setter function is being called from inside or outside of the setter's class. this approach should allow me to maintain a public property but with more security, something like public/private hybrid access.
is it possible to use arguments' callee property, or perhaps something else, to determine whether the setter was set internally from the setter's class or if it was set externally?
here's an example of what i mean:
package
{
//Class
public class MyLocation
{
//Constants
public static const LOCATION_LEFT:String = "locationLeft";
public static const LOCATION_RIGHT:String = "locationRight";
//Properties
private var sideLocationProperty:String;
//Constructor
public function MyLocation(sideLocation:String = MyLocation.LOCATION_LEFT)
{
this.sideLocation = sideLocation;
trace(sideLocation);
}
//Texture Panel Is Collapsed Setter
public function set sideLocation(value:String):void
{
if (value != LOCATION_LEFT && value != LOCATION_RIGHT && value != LOCATION_AUTO)
throw new ArgumentError("\"MyLocation set sideLocation(value:String)\" – value parameter is not supported.");
sideLocationProperty = value;
//pseudo code
if (arguments.callee is from MyLocation)
trace("sideLocation was set internally");
else
trace("sideLocation was set externally");
}
//Texture Panel Is Collapsed Getter
public function get sideLocation():String
{
return sideLocationProperty;
}
}
}
Upvotes: 0
Views: 227
Reputation: 546
This is an old questions but if someone else is looking... try the following and parse the stack trace to get the caller
trace( new Error().getStackTrace() )
Upvotes: 0
Reputation: 1937
Yeah, AS3 getters and setters are already flexible enough to make it easy to write difficult-to-follow code by making them unexpected side effects, and this idea would just make it even more confusing. The whole point of a getter or setter is to provide a controlled public access point--there is no need to call a getter or setter from within the class that has access to the private members.
Upvotes: 0
Reputation: 2220
Most likely you can't do that.
Why don't you write
public function setSideLocation(value:String, caller:* = null):void
then you can do whatever you want to do with it, like
trace(caller is MovieClip);
or caller.hasOwnProperty(something)
etc.
By setting it to null as a default value, you don't even need to use it. Just optionally.
So to call it from another object do:
object.setSideLocation("something", this);
Also to keep it consistent write
public function getSideLocation():String
.
Upvotes: 3
Reputation: 9198
No, not possible.
Also, it's a really bad idea. try to keep your code free of side-effects, ie. you shouldn't write code that deliberately or accidentally performs differently because of the Class of the caller. Imagine trying to unit-test such code - nightmare.
Upvotes: 2