Is it possible to add behavior to a non-dynamic ActionScript 3 class without class inheritance?

What I would like to do is something like the following:

FooClass.prototype.method = function():String
{
    return "Something";
}

var foo:FooClass = new FooClass();
foo.method();

That is, I would like to extend the generated class with one method, and not through inheritance, but through a prototype.

The class is created from WSDL, it is not a dynamic class, and I do not want to touch the generated code, because it will be overwritten anyway.

In short, I would like to have the moral equivalent of C # 3: s Extension Methods for AS3.

Edit: I accepted the answer from aib because it matches what I asked best - although with further thought it really does not solve my problem, but it’s my fault that you asked the wrong question. :) Also, upmods are for good suggestions.

+5
5

, .

, .

Try

foo["method"]();

foo.method();
+3

@Theo: , 3.0.0.477 flex-config.xml (<strict> true </strict> ) -compiler.strict, mxmlc?

Foo.as:

package
{
    public class Foo
    {
        public var foo:String;

        public function Foo()
        {
            foo = "foo!";
        }
    }
}

footest.as:

package
{
    import flash.display.Sprite;

    public class footest extends Sprite
    {
        public function footest()
        {
            Foo.prototype.method = function():String
            {
                return "Something";
            }

            var foo:Foo = new Foo();
            trace(foo["method"]());
        }
    }
}

, OP , . ( , "" , , .)

+4

, , :

:

public class SampleClass
{
    public function SampleClass()
    {
    }

    public function method1():void {
        Alert.show("Hi");
    }

Quick Wrapper:

var actualClass:SampleClass = new SampleClass();

var QuickWrapper:Object = {
    ref: actualClass,
    method1: function():void {
        this.ref.method1();
    },
    method2: function():void {
        Alert.show("Hello!");
    }   
};

QuickWrapper.method1();
QuickWrapper.method2();
+2

: @aib, , . ( ), ActionScript 3. , .

? , , -, , :

public class FooWrapper extends Foo {

    private var wrappedFoo : Foo;

    public function FooWrapper( foo : Foo ) {
        wrappedFoo = foo;
    }

    override public function methodFromFoo( ) : void {
        wrappedFoo.methodFromFoo();
    }

    override public function anotherMethodFromFoo( ) : void {
        wrappedFoo.anotherMethodFromFoo();
    }

    public function newMethodNotOnFoo( ) : String {
        return "Hello world!"
    }

}

Foo, , Foo FooWrapper .

, , , FooWrapper , , , , , , .

, . , , WSDL, , .

+1

- () .

For example, suppose you don't like the fact that Flex 3 SpriteAsset.as returns the default metrics by default [7,7,7,7] (as opposed to flex 2). To fix this, you can:

  • Create a copy of SpriteAsset.as and add it to your project at /mx/core/SpriteAsset.as
  • Edit the local copy to fix any problems you find.
  • Run ap

Google " flex alkey patch " for more examples and instructions.

+1
source

All Articles