引用的getter / setter函数在ActionScript 3函数、getter、setter、ActionScript

2023-09-08 13:12:39 作者:独宠一人

一个人如何在ActionScript 3中得到一个参考的getter和setter函数?

如果一个方法的调用定义的,例如

 公共职能胡说():字符串{...}
 

我可以只是说获得对它的引用等等 this.blah

如何获得引用

 公共职能得到blah2():字符串{}
公共功能集blah2(二:字符串):无效{}
 
函数实践拓展 支付宝蚂蚁森林能量计算

谢谢!

解决方案

原始响应:

不幸的是,你将无法引用存储作为这些功能。 getter和setter方法​​实际上是建立在,你不应该是能够的想法,因此,他们作为一个属性。

有没有你需要引用的功能具体是什么原因呢?

注释我回答:

我想根据自定义元数据标签,如动态添加外部接口方法[外部]。我能做到这一点的常规方法,但我想这延伸到的getter / setter方法​​为好。要做到这一点,我需要一个引用函数动态,这样我就可以用正确的args使用应用函数执行它。

我觉得你最好使用在这种情况下,一个多步骤的方法。由于getter和setter作为属性,而不是一种方法,它会是有意义的测试,看它是否是一个属性,然后干脆直接给它分配一个值。您是否能够使用这样的:

 如果(foo.blah2是功能)
{
    foo.blah2.apply(FOO,ARR);
}
其他
{
    foo.blah2 =改编[0];
}
 

How does one get a reference the the getter and setter functions in actionscript 3?

if a method is defined on the calls, e.g.

public function blah():String { ...}

I can get a reference to it by just saying blah or this.blah

How do get a reference to

public function get blah2():String {}
public function set blah2(b:String):void {}

Thanks!

解决方案

Original response:

Unfortunately, you will not be able to store references to those as functions. The getter and setter methods are actually built around the idea that you shouldn't be able to and they therefore function as a property.

Is there a reason that you need to reference the functions specifically?

The comment I'm responding to:

I want to dynamically add external interface methods based on custom metadata tags, e.g. [External]. I was able to do this for the regular methods, but I'm trying to extend this to getter/setters as well. To do this, I need to get a reference to the function dynamically, so I can execute it with the right args using the apply function.

I think you're better off using a multi-step approach in that case. Since getters and setters function as a property and not a method, it would make sense to test to see if it is a property and then simply assign it a value directly. Would you be able to use this:

if( foo.blah2 is Function )
{
    foo.blah2.apply( foo, arr );
}
else
{
    foo.blah2 = arr[ 0 ];
}