oo.js 3.9 KB

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