AS2:设置新属性时调用方法
我有一个动态类,我想做的是每次在运行时将属性附加到类时调用一个方法。
例如:
dynamic class Example
{
public function Example()
{
trace("New instance created");
}
public function setter(name:String, value:String):Void
{
trace("Property '"+name+"' created with value '"+value+"'");
}
}
然后从时间线中,当我向示例添加新属性时:
Example.newProperty = "some value";
我希望它跟踪:
使用值“some value”创建的属性“newProperty”
我完全意识到,这可以通过使用函数来设置属性,如下所示:
public function setter(name:String, value:String):Void
{
this[name] = "some value";
trace("Property '"+name+"' created with value '"+value+"'");
}
并像这样调用它:
Example.setter("newProperty", "some value");
但是,我希望当通过添加属性时自动触发此方法常规的 .dot 运算符,不必显式调用函数。
这可能吗?
I have a dynamic Class
and what I would like to do is call a method everytime a property is appended to the class during run-time.
For example:
dynamic class Example
{
public function Example()
{
trace("New instance created");
}
public function setter(name:String, value:String):Void
{
trace("Property '"+name+"' created with value '"+value+"'");
}
}
And then from the timeline when I would add a new property to Example:
Example.newProperty = "some value";
I want it to trace:
Property 'newProperty' created with value ' some value'
I am fully aware that this is capable by using a function to set properties like so:
public function setter(name:String, value:String):Void
{
this[name] = "some value";
trace("Property '"+name+"' created with value '"+value+"'");
}
and calling it like so:
Example.setter("newProperty", "some value");
However I want this method to fire automatically when a property is added via the regular .dot
operator and not have to call a function explicitly.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
既然它是AS2,那么,是的,你的类必须实现
__resolve(x)
方法。然而,我认为这是一个非常值得怀疑的设计决定。接受键和值的函数对我来说看起来好多了,而且最终,它的代码更少。Since it's AS2, then, yes, your class has to implement
__resolve(x)
method. I would, however, consider it a very questionable design decision. The function that accepts the key and the value looks much better to me, and, in the end, it's less code.可以使用
代理
< /a> 这里。示例类:
演示代码:
输出:
Could use
Proxy
here.Example class:
Demo code:
Output: