cast.js 1003 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var RTL$ = {
  2. extend: function extend(methods){
  3. methods.__proto__ = this.prototype; // make instanceof work
  4. // to see constructor name in diagnostic
  5. var result = methods.init;
  6. methods.constructor = result.prototype.constructor;
  7. result.prototype = methods;
  8. result.extend = extend;
  9. return result;
  10. },
  11. typeGuard: function RTLTypeGuard(from, to){
  12. if (!(from instanceof to))
  13. throw new Error("typeguard assertion failed");
  14. return from;
  15. }
  16. };
  17. var m = function (){
  18. var Base = RTL$.extend({
  19. init: function Base(){
  20. }
  21. });
  22. var Derived1 = Base.extend({
  23. init: function Derived1(){
  24. Base.prototype.init.bind(this)();
  25. this.field1 = 0;
  26. }
  27. });
  28. var Derived2 = Derived1.extend({
  29. init: function Derived2(){
  30. Derived1.prototype.init.bind(this)();
  31. this.field2 = 0;
  32. }
  33. });
  34. var pb = null;
  35. var pd1 = null;
  36. var pd2 = null;
  37. pd2 = new Derived2();
  38. pb = pd2;
  39. pd1 = pd2;
  40. RTL$.typeGuard(pb, Derived1).field1 = 0;
  41. RTL$.typeGuard(pb, Derived2).field2 = 1;
  42. RTL$.typeGuard(pd1, Derived2).field2 = 2;
  43. }();