ActionScript - 确定调用者的类?
是否可以使用参数的被调用者属性或其他属性来确定 setter 是从 setter 的类内部设置的还是外部设置的?
这是我的意思的一个例子:
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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您很可能无法做到这一点。
你为什么不这样写,
然后你就可以用它做任何你想做的事情,比如
或
caller.hasOwnProperty(something)
等。
通过将其设置为 null 作为默认值,你甚至不需要需要使用它。只是可选的。
因此,要从另一个对象调用它,请执行以下操作:
另外,为了保持一致,请编写
public function getSideLocation():String
。Most likely you can't do that.
Why don't you write
then you can do whatever you want to do with it, like
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:
Also to keep it consistent write
public function getSideLocation():String
.不,不可能。
而且,这是一个非常糟糕的主意。尽量让你的代码没有副作用,即。您不应该编写因调用者的类而故意或意外执行不同的代码。想象一下尝试对这样的代码进行单元测试 - 噩梦。
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.
是的,AS3 getter 和 setter 已经足够灵活,可以通过产生意想不到的副作用来轻松编写难以遵循的代码,而这个想法只会让它变得更加混乱。 getter 或 setter 的全部目的是提供受控的公共访问点——无需从有权访问私有成员的类中调用 getter 或 setter。
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.
这是一个老问题,但如果其他人正在寻找......请尝试以下操作并解析堆栈跟踪以获取调用者
This is an old questions but if someone else is looking... try the following and parse the stack trace to get the caller