oo.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // wrapped by build app
  2. define("dojox/drawing/util/oo", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.util.oo");
  4. // TODO:
  5. // allow a declare without a mixin
  6. dojox.drawing.util.oo = {
  7. // summary:
  8. // Inheritance utilities used in DojoX Drawing
  9. // description:
  10. // Inheritance utilities used in DojoX Drawing.
  11. // There were designed in a effort to make Drawing as
  12. // fast as possible - especially in a case where thousands
  13. // of objects are being loaded. Drawing declare performs
  14. // about 3 times faster than Dojo declare and 2 times
  15. // faster than Dojox declare. This is not to say Drawing
  16. // declare is wthout limitations. It doesn't have the same
  17. // syntatic sugar and extensibility of the other two. You
  18. // can't inhert methods. It won't work with Dijit. But it
  19. // is simple and effective.
  20. //
  21. declare: function(){
  22. // summary:
  23. // Creates a constructor Function from a
  24. // Function, and collection of methods, and
  25. // more Functions that are extended.
  26. // description:
  27. // Similar in look and feel to Dojo declare as
  28. // far as order and number of arguments, although
  29. // constructed a little closer to prototypical
  30. // inheritance. All arguments passed into the
  31. // constructor are passed into all sub constructors.
  32. // arguments:
  33. // Function, [Object|Function....]
  34. // The first argument is always the base
  35. // constructor. The last argument is always
  36. // an object of methods (or empty object) to
  37. // be mixed in (in the future would like to
  38. // make that object optional). Remaining
  39. // arguments are other constructors mixed in
  40. // using extend() (See below).
  41. // example:
  42. // | MyFunction = dojox.drawing.util.oo.declare(
  43. // | MyOtherFunction,
  44. // | YetAnotherFunction,
  45. // | function(options){
  46. // | // This is my constructor. It will fire last.
  47. // | // The other constructors will fire before this.
  48. // | },
  49. // | {
  50. // | customType:"equation", // mixed in property
  51. // | doThing: function(){ // mixed in method
  52. // |
  53. // | }
  54. // | }
  55. // | );
  56. // |
  57. // | var f = new MyFunction();
  58. //
  59. var f, o, ext=0, a = arguments;
  60. if(a.length<2){ console.error("drawing.util.oo.declare; not enough arguments")}
  61. if(a.length==2){
  62. f = a[0]; o = a[1];
  63. }else{
  64. a = Array.prototype.slice.call(arguments);
  65. o = a.pop();
  66. f = a.pop();
  67. ext = 1;
  68. }
  69. for(var n in o){
  70. f.prototype[n] = o[n];
  71. }
  72. if(ext){
  73. a.unshift(f);
  74. f = this.extend.apply(this, a);
  75. }
  76. return f; // Function
  77. },
  78. extend: function(){
  79. // summary:
  80. // Extends constructors to inherit from other
  81. // constructors .
  82. // description:
  83. // Typically not used by itself - it's used as
  84. // part of declare(). Could be used by itself
  85. // however, to mix together two or more
  86. // constructors.
  87. // arguments:
  88. // Function, [ Function...]
  89. // Any number of arguments, all must be
  90. // function constructors. The first is
  91. // considered the base object and its
  92. // constructor will fire first.
  93. // example:
  94. // | var A = function(){};
  95. // | var B = function(){};
  96. // | var C = function(){};
  97. // | var D = dojox.drawing.util.oo.extend(A, B, C);
  98. // | var e = new D();
  99. //
  100. var a = arguments, sub = a[0];
  101. if(a.length<2){ console.error("drawing.util.oo.extend; not enough arguments")}
  102. var f = function (){
  103. for(var i=1;i<a.length;i++){
  104. a[i].prototype.constructor.apply(this, arguments);
  105. }
  106. // sub should fire last
  107. sub.prototype.constructor.apply(this, arguments);
  108. }
  109. for(var i=1;i<a.length;i++){
  110. for(var n in a[i].prototype){
  111. f.prototype[n] = a[i].prototype[n];
  112. }
  113. }
  114. for(n in sub.prototype){
  115. f.prototype[n] = sub.prototype[n];
  116. }
  117. return f; // Function
  118. }
  119. };
  120. });