<p>What I'd like to do is something like the following:</p>

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

var foo:FooClass = new FooClass();
foo.method();
</code></pre>

<p>Which is to say, I'd like to extend a generated class with a single method, not via inheritance but via the prototype.</p>

<p>The class is generated from a WSDL, it's not a dynamic class, and I don't want to touch the generated code because it will be overwritten anyway.</p>

<p>Long story short, I'd like to have the moral equivalent of C# 3:s Extension Methods for AS3.</p>

<p>Edit: I accepted aib's answer, because it fits what I was asking best -- although upon further reflection it doesn't really solve my problem, but that's my fault for asking the wrong question. :) Also, upmods for the good suggestions.</p>

[{'answer_id': 8886, 'author': 'aib', 'author_id': 1088, 'author_profile': 'https://Stackoverflow.com/users/1088', 'pm_score': 3, 'selected': True, 'text': '<p>Yes, such a thing is possible.</p>\n\n<p>In fact, your example is very close to the solution.</p>\n\n<p>Try</p>\n\n<pre><code>foo["method"]();\n</code></pre>\n\n<p>instead of</p>\n\n<pre><code>foo.method();\n</code></pre>\n'}, {'answer_id': 9958, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 1, 'selected': False, 'text': '<p>@aib is unfortunately incorrect. Assuming strict mode (the default compiler mode) it is not possible to modify the prototype of non-dynamic class types in ActionScript 3. I\'m not even sure that it\'s possible in non-strict mode.</p>\n\n<p>Is wrapping an option? Basically you create a class that takes one of the objects you get from the web service and just forwards all method calls to that, but also has methods of its own:</p>\n\n<pre><code>public class FooWrapper extends Foo {\n\n    private var wrappedFoo : Foo;\n\n    public function FooWrapper( foo : Foo ) {\n        wrappedFoo = foo;\n    }\n\n    override public function methodFromFoo( ) : void {\n        wrappedFoo.methodFromFoo();\n    }\n\n    override public function anotherMethodFromFoo( ) : void {\n        wrappedFoo.anotherMethodFromFoo();\n    }\n\n    public function newMethodNotOnFoo( ) : String {\n        return "Hello world!"\n    }\n\n}\n</code></pre>\n\n<p>When you want to work with a <code>Foo</code>, but also have the extra method you need you wrap the <code>Foo</code> instance in a <code>FooWrapper</code> and work with that object instead.</p>\n\n<p>It\'s not the most convenient solution, there\'s a lot of typing and if the generated code changes you have to change the <code>FooWrapper</code> class by hand, but unless you can modify the generated code either to include the method you want or to make the class dynamic I don\'t see how it can be done.</p>\n\n<p>Another solution is to add a step to your build process that modifies the source of the generated classes. I assume that you already have a step that generates the code from a WSDL, so what you could do is to add a step after that that inserts the methods you need.</p>\n'}, {'answer_id': 10884, 'author': 'aib', 'author_id': 1088, 'author_profile': 'https://Stackoverflow.com/users/1088', 'pm_score': 2, 'selected': False, 'text': '<p>@Theo: How would you explain the following working in 3.0.0.477 with the default flex-config.xml (&lt;strict&gt;true&lt;/strict&gt;) and even a -compiler.strict parameter passed to mxmlc?</p>\n\n<p>Foo.as:</p>\n\n<pre><code>package\n{\n    public class Foo\n    {\n        public var foo:String;\n\n        public function Foo()\n        {\n            foo = "foo!";\n        }\n    }\n}\n</code></pre>\n\n<p>footest.as:</p>\n\n<pre><code>package\n{\n    import flash.display.Sprite;\n\n    public class footest extends Sprite\n    {\n        public function footest()\n        {\n            Foo.prototype.method = function():String\n            {\n                return "Something";\n            }\n\n            var foo:Foo = new Foo();\n            trace(foo["method"]());\n        }\n    }\n}\n</code></pre>\n\n<p>Note that the OP said inheritance was unacceptable, as was modifying the generated code. (If that weren\'t the case, adding "dynamic" to the class definition would probably be the easiest solution.)</p>\n'}, {'answer_id': 11259, 'author': 'Matt MacLean', 'author_id': 22, 'author_profile': 'https://Stackoverflow.com/users/22', 'pm_score': 2, 'selected': False, 'text': '<p>Depending on how many methods your class has, this may work:</p>\n\n<p>Actual Class:</p>\n\n<pre><code>public class SampleClass\n{\n    public function SampleClass()\n    {\n    }\n\n    public function method1():void {\n        Alert.show("Hi");\n    }\n</code></pre>\n\n<p>Quick Wrapper:</p>\n\n<pre><code>var actualClass:SampleClass = new SampleClass();\n\nvar QuickWrapper:Object = {\n    ref: actualClass,\n    method1: function():void {\n        this.ref.method1();\n    },\n    method2: function():void {\n        Alert.show("Hello!");\n    }   \n};\n\nQuickWrapper.method1();\nQuickWrapper.method2();\n</code></pre>\n'}, {'answer_id': 87092, 'author': 'Brian', 'author_id': 1750627, 'author_profile': 'https://Stackoverflow.com/users/1750627', 'pm_score': 1, 'selected': False, 'text': '<p>Monkey patching is an (inelegant) option.</p>\n\n<p>For example, suppose you don\'t like the fact that Flex 3 SpriteAsset.as returns a default border metrics of [7,7,7,7] (unlike flex 2). To fix this, you can:</p>\n\n<ol>\n<li>Create a copy of SpriteAsset.as and add it to your project at /mx/core/SpriteAsset.as</li>\n<li>Edit your local copy to fix any problems you find</li>\n<li>Run your ap</li>\n</ol>\n\n<p>Google "<a href="http://www.google.com/search?q=flex+monkey+patch" rel="nofollow noreferrer">flex monkey patch</a>" for more examples and instructions.</p>\n'}]
['https://Stackoverflow.com/questions/7212', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/266/']