AS2:设置新属性时调用方法

发布于 2025-01-03 02:18:32 字数 877 浏览 3 评论 0原文

我有一个动态类,我想做的是每次在运行时将属性附加到类时调用一个方法。

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2025-01-10 02:18:32

既然它是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.

ぺ禁宫浮华殁 2025-01-10 02:18:32

可以使用 代理< /a> 这里。

示例类:

package
{
    import flash.utils.Proxy;
    import flash.utils.flash_proxy;

    dynamic public class Example extends Proxy
    {

        private var _properties:Object = {};


        override flash_proxy function setProperty(name:*, value:*):void
        {
            _properties[name] = value;
            trace("Property '" + name + "' created with value '" + value + "'");
        }


        override flash_proxy function getProperty(name:*):*
        {
            return _properties[name];
        }

    }
}

演示代码:

var ex:Example = new Example();

ex.something = 10;
ex.more = "something more";

trace(ex.something);
trace(ex.more);

输出:

使用值“10”创建的属性“something”
财产“更多”创造了价值“更多”
10
更多内容

Could use Proxy here.

Example class:

package
{
    import flash.utils.Proxy;
    import flash.utils.flash_proxy;

    dynamic public class Example extends Proxy
    {

        private var _properties:Object = {};


        override flash_proxy function setProperty(name:*, value:*):void
        {
            _properties[name] = value;
            trace("Property '" + name + "' created with value '" + value + "'");
        }


        override flash_proxy function getProperty(name:*):*
        {
            return _properties[name];
        }

    }
}

Demo code:

var ex:Example = new Example();

ex.something = 10;
ex.more = "something more";

trace(ex.something);
trace(ex.more);

Output:

Property 'something' created with value '10'
Property 'more' created with value 'something more'
10
something more

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文