1 | dojo.provide("dojox.drawing.util.oo"); |
---|
2 | |
---|
3 | // TODO: |
---|
4 | // allow a declare without a mixin |
---|
5 | |
---|
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 | |
---|
61 | if(a.length<2){ console.error("drawing.util.oo.declare; not enough arguments")} |
---|
62 | if(a.length==2){ |
---|
63 | f = a[0]; o = a[1]; |
---|
64 | }else{ |
---|
65 | a = Array.prototype.slice.call(arguments); |
---|
66 | o = a.pop(); |
---|
67 | f = a.pop(); |
---|
68 | ext = 1; |
---|
69 | } |
---|
70 | for(var n in o){ |
---|
71 | f.prototype[n] = o[n]; |
---|
72 | } |
---|
73 | if(ext){ |
---|
74 | a.unshift(f); |
---|
75 | f = this.extend.apply(this, a); |
---|
76 | } |
---|
77 | return f; // Function |
---|
78 | }, |
---|
79 | extend: function(){ |
---|
80 | // summary: |
---|
81 | // Extends constructors to inherit from other |
---|
82 | // constructors . |
---|
83 | // description: |
---|
84 | // Typically not used by itself - it's used as |
---|
85 | // part of declare(). Could be used by itself |
---|
86 | // however, to mix together two or more |
---|
87 | // constructors. |
---|
88 | // arguments: |
---|
89 | // Function, [ Function...] |
---|
90 | // Any number of arguments, all must be |
---|
91 | // function constructors. The first is |
---|
92 | // considered the base object and its |
---|
93 | // constructor will fire first. |
---|
94 | // example: |
---|
95 | // | var A = function(){}; |
---|
96 | // | var B = function(){}; |
---|
97 | // | var C = function(){}; |
---|
98 | // | var D = dojox.drawing.util.oo.extend(A, B, C); |
---|
99 | // | var e = new D(); |
---|
100 | // |
---|
101 | var a = arguments, sub = a[0]; |
---|
102 | if(a.length<2){ console.error("drawing.util.oo.extend; not enough arguments")} |
---|
103 | var f = function (){ |
---|
104 | for(var i=1;i<a.length;i++){ |
---|
105 | a[i].prototype.constructor.apply(this, arguments); |
---|
106 | } |
---|
107 | // sub should fire last |
---|
108 | sub.prototype.constructor.apply(this, arguments); |
---|
109 | |
---|
110 | } |
---|
111 | for(var i=1;i<a.length;i++){ |
---|
112 | for(var n in a[i].prototype){ |
---|
113 | f.prototype[n] = a[i].prototype[n]; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | for(n in sub.prototype){ |
---|
118 | f.prototype[n] = sub.prototype[n]; |
---|
119 | } |
---|
120 | return f; // Function |
---|
121 | } |
---|
122 | }; |
---|