fork download
  1.  
  2. // Объект включающий в себя функционал по проверки типов переменных.
  3. var TypeOf = new (function()
  4. {
  5. {
  6. // Строковые представления JS-типов
  7.  
  8. var m_number = (typeof 0);
  9. var m_object = (typeof {});
  10. var m_string = (typeof '');
  11. var m_boolean = (typeof true);
  12. var m_undefined = (typeof undefined);
  13. var m_function = (typeof (function(){}));
  14. }
  15.  
  16. {
  17. // Если не нравится вызов методов для проверки можно использовать нижеприведенные переменные.
  18. // Но чисто теоретически их можно изменять из вне что приведет к глюку всей программы
  19.  
  20. this.Number = m_number;
  21. this.Object = m_object;
  22. this.String = m_string;
  23. this.Boolean = m_boolean;
  24. this.Undefined = m_undefined;
  25. this.Function = m_function;
  26. }
  27.  
  28. {
  29. // Методы по проверки типов переменных
  30.  
  31. this.isNumber = function(value) { return (typeof value == m_number); };
  32. this.isNotNumber = function(value) { return (typeof value != m_number); };
  33. this.isObject = function(value) { return (typeof value == m_object); };
  34. this.isNotObject = function(value) { return (typeof value != m_object); };
  35. this.isString = function(value) { return (typeof value == m_string); };
  36. this.isNotString = function(value) { return (typeof value != m_string); };
  37. this.isBoolean = function(value) { return (typeof value == m_boolean); };
  38. this.isNotBoolean = function(value) { return (typeof value != m_boolean); };
  39. this.isUndefined = function(value) { return (typeof value == m_undefined); };
  40. this.isNotUndefined = function(value) { return (typeof value != m_undefined); };
  41. this.isFunction = function(value) { return (typeof value == m_function); };
  42. this.isNotFunction = function(value) { return (typeof value != m_function); };
  43. }
  44. })();
  45.  
  46.  
  47.  
  48. function setSuperclass(Child, Parent)
  49. {
  50. var F = function() { }
  51. F.prototype = Parent.prototype
  52. Child.prototype = new F()
  53. Child.prototype.constructor = Child
  54. Child.superclass = Parent.prototype
  55. }
  56.  
  57. // Получить объект-прототип данному созданный на основе конструктора Constructor
  58. this.__proto__.protoToConstructor = function(Constructor)
  59. {
  60. var object = this.__proto__;
  61. while (object instanceof Object && object.constructor != Constructor) object = object.__proto__;
  62. return (object instanceof Object && object.constructor == Constructor) ? object : null;
  63. }
  64.  
  65.  
  66.  
  67. function extend(Child, Parent)
  68. {
  69. var F = function() { }
  70. F.prototype = Parent.prototype
  71. Child.prototype = new F()
  72. Child.prototype.constructor = Child
  73. Child.superclass = Parent.prototype
  74. }
  75.  
  76.  
  77. function DataPrototype()
  78. {
  79. this.toString = function() { return '[object ' + this.constructor.name + (this.toStringParam() != null ? (': ' + this.toStringParam()) : '') + ']'; }
  80.  
  81. this.toStringParam = function() { return null; }
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. setSuperclass(Obj1, DataPrototype);
  90. function Obj1()
  91. {
  92. Obj1.superclass.constructor();
  93. }
  94.  
  95.  
  96. setSuperclass(Obj2, Obj1);
  97. function Obj2()
  98. {
  99. Obj2.superclass.constructor();
  100. }
  101.  
  102.  
  103. setSuperclass(Obj3, Obj2);
  104. function Obj3()
  105. {
  106. Obj3.superclass.constructor();
  107.  
  108. this.toStringParam = function() { return 'ыть-3'; }
  109. }
  110.  
  111.  
  112. setSuperclass(Obj4, Obj3);
  113. function Obj4()
  114. {
  115. Obj4.superclass.constructor();
  116. }
  117.  
  118.  
  119. setSuperclass(Obj5, Obj4);
  120. function Obj5()
  121. {
  122. Obj5.superclass.constructor();
  123.  
  124. this.toStringParam = function() { return 'ыть-5'; }
  125.  
  126. this.test = function() { print(this.protoToConstructor(Obj2)); }
  127. }
  128.  
  129.  
  130.  
  131. print(new Obj1());
  132. print(new Obj2());
  133. print(new Obj3());
  134. print(new Obj4());
  135. print(new Obj5());
  136.  
  137. print('------------');
  138.  
  139. print((new Obj4()).protoToConstructor(Obj3));
  140. (new Obj5()).test();
  141.  
Success #stdin #stdout 0.01s 4984KB
stdin
Standard input is empty
stdout
[object Obj1]
[object Obj2]
[object Obj3: ыть-3]
[object Obj4: ыть-3]
[object Obj5: ыть-5]
------------
[object Obj3: ыть-3]
[object Obj2]