AS3:重写代理 getProperty 方法
我在尝试使用 Proxy 类并重写 getProperty() 方法时遇到了一个特殊问题。我在下面附上了我的示例类代码:
package
{
import flash.utils.Proxy;
import flash.utils.flash_proxy;
public class Thing extends Proxy
{
// holder object
private var _holder:Object;
/**
* Constructor
*/
public function Thing()
{
_holder =
{
stuff: "thing"
};
}
/**
* Override getProperty
*/
override flash_proxy function getProperty(name:*):*
{
trace(name + " being accessed");
return _holder[name];
}
}
}
我一直在尝试使用它来使某些属性只读(根据 我的上一个问题),但是有一些奇怪的行为我似乎无法解决。
使用上面的内容,我尝试像这样访问变量 stuff
:
var t:Thing = new Thing();
trace(t.stuff);
但是,这会引发以下错误:
1119:通过引用访问可能未定义的属性内容 具有静态类型 Thing。
但如果我这样做:
trace(t["stuff"]);
效果很好。我做错了什么?
I've run into a peculiar problem while trying to make use of the Proxy
class and override the getProperty()
method. I've attached my example class code below:
package
{
import flash.utils.Proxy;
import flash.utils.flash_proxy;
public class Thing extends Proxy
{
// holder object
private var _holder:Object;
/**
* Constructor
*/
public function Thing()
{
_holder =
{
stuff: "thing"
};
}
/**
* Override getProperty
*/
override flash_proxy function getProperty(name:*):*
{
trace(name + " being accessed");
return _holder[name];
}
}
}
I've been trying to use this to make some properties read-only (as per an answer on a previous question of mine), however there's some odd behaviour that I can't seem to work out.
Using the above, I try and access the variable stuff
like so:
var t:Thing = new Thing();
trace(t.stuff);
However this throws the following error:
1119: Access of possibly undefined property stuff through a reference
with static type Thing.
But if I do this:
trace(t["stuff"]);
It works fine. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为你的类不是动态的,只需添加“dynamic”关键字,一切都会好的:
This is because your class isn't dynamic, just add "dynamic" keyword and all will be good :