views:

58

answers:

0

hi,
I'm working on a Actionscript 3.0 to Javascript-converter.
I tried to find a way to circumvent the problem with the order of definitions, because it doesn't matter in AS, which package is defined first. To my surprise the following code works, although the prototype of A is defined after BA inherits from A:


javascript (example output)

Function.prototype.inherit = function(base) {
   var empty = Function.prototype.inherit.empty;
   empty.prototype = base.prototype;
   this.prototype = new empty();
}
Function.prototype.inherit.empty = function() {};

//order doesn't matter
(function(undefined) {
   function BA() {}
   BA.inherit(A);
   BA.prototype.f1 = function() { return "success"; }

   function A() {}
   A.prototype.f1 = function() { return "fail"; }
   A.prototype.f2 = function() { return "success"; }

   var bA = new BA;
   console.log("test1: %s; %s", bA.f1(), bA.f2());
   console.assert(bA instanceof BA);
   console.assert(bA instanceof A);
})();



Today I started to test subtyping and unfortunately the order of definitions plays a role in that case:


javascript (example output)


//order plays a decisive role
(function(undefined) {
   function CBA() {}
   CBA.inherit(BA);
   CBA.prototype.constructor = CBA;
   CBA.prototype.f3 = function() { return "success"; }

   function BA() {}
   BA.inherit(A);
   BA.prototype.constructor = BA;
   BA.prototype.f1 = function() { return "success"; }

   function A() {}
   A.prototype.f1 = function() { return "fail"; }
   A.prototype.f2 = function() { return "success"; }

   var cBA = new CBA;
   //this won't work. The compiler claims that cBA.f1 is not a function
   console.log("test2: %s; %s, %s", cBA.f1(), cBA.f2(), cBA.f3());
})();



How can i circumvent this problem? Note that I'm not going to use any libraries which provide inheritance.
It would be even a big help, if someone tells me whether Actionscript 3.0 supports mutually importing / accessing packages, e.g.:
actionscript


package my.pkg1 {
   class A {
   }
   class B extends my.pkg2.C {
   }
package my.pkg2 {
   class C {
   }
   class D extends my.pkg1.A {
   }
}

thanks.
Uli