code_junkie

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

Nov 10th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. FooClass.prototype.method = function():String
  2. {
  3. return "Something";
  4. }
  5.  
  6. var foo:FooClass = new FooClass();
  7. foo.method();
  8.  
  9. foo["method"]();
  10.  
  11. foo.method();
  12.  
  13. package
  14. {
  15. public class Foo
  16. {
  17. public var foo:String;
  18.  
  19. public function Foo()
  20. {
  21. foo = "foo!";
  22. }
  23. }
  24. }
  25.  
  26. package
  27. {
  28. import flash.display.Sprite;
  29.  
  30. public class footest extends Sprite
  31. {
  32. public function footest()
  33. {
  34. Foo.prototype.method = function():String
  35. {
  36. return "Something";
  37. }
  38.  
  39. var foo:Foo = new Foo();
  40. trace(foo["method"]());
  41. }
  42. }
  43. }
  44.  
  45. public class FooWrapper extends Foo {
  46.  
  47. private var wrappedFoo : Foo;
  48.  
  49. public function FooWrapper( foo : Foo ) {
  50. wrappedFoo = foo;
  51. }
  52.  
  53. override public function methodFromFoo( ) : void {
  54. wrappedFoo.methodFromFoo();
  55. }
  56.  
  57. override public function anotherMethodFromFoo( ) : void {
  58. wrappedFoo.anotherMethodFromFoo();
  59. }
  60.  
  61. public function newMethodNotOnFoo( ) : String {
  62. return "Hello world!"
  63. }
  64.  
  65. }
  66.  
  67. public class SampleClass
  68. {
  69. public function SampleClass()
  70. {
  71. }
  72.  
  73. public function method1():void {
  74. Alert.show("Hi");
  75. }
  76.  
  77. var actualClass:SampleClass = new SampleClass();
  78.  
  79. var QuickWrapper:Object = {
  80. ref: actualClass,
  81. method1: function():void {
  82. this.ref.method1();
  83. },
  84. method2: function():void {
  85. Alert.show("Hello!");
  86. }
  87. };
  88.  
  89. QuickWrapper.method1();
  90. QuickWrapper.method2();
Add Comment
Please, Sign In to add comment