1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * Constructor: OpenLayers.Class
7 * Base class used to construct all other classes. Includes support for
8 * multiple inheritance.
10 * This constructor is new in OpenLayers 2.5. At OpenLayers 3.0, the old
11 * syntax for creating classes and dealing with inheritance
14 * To create a new OpenLayers-style class, use the following syntax:
15 * > var MyClass = OpenLayers.Class(prototype);
17 * To create a new OpenLayers-style class with multiple inheritance, use the
19 * > var MyClass = OpenLayers.Class(Class1, Class2, prototype);
20 * Note that instanceof reflection will only reveil Class1 as superclass.
21 * Class2 ff are mixins.
24 OpenLayers.Class = function() {
25 var Class = function() {
27 * This following condition can be removed at 3.0 - this is only for
28 * backwards compatibility while the Class.inherit method is still
29 * in use. So at 3.0, the following three lines would be replaced with
31 * this.initialize.apply(this, arguments);
33 if (arguments && arguments[0] != OpenLayers.Class.isPrototype) {
34 this.initialize.apply(this, arguments);
38 var parent, initialize;
39 for(var i=0, len=arguments.length; i<len; ++i) {
40 if(typeof arguments[i] == "function") {
41 // make the class passed as the first argument the superclass
42 if(i == 0 && len > 1) {
43 initialize = arguments[i].prototype.initialize;
44 // replace the initialize method with an empty function,
45 // because we do not want to create a real instance here
46 arguments[i].prototype.initialize = function() {};
47 // the line below makes sure that the new class has a
49 extended = new arguments[i];
50 // restore the original initialize method
51 if(initialize === undefined) {
52 delete arguments[i].prototype.initialize;
54 arguments[i].prototype.initialize = initialize;
57 // get the prototype of the superclass
58 parent = arguments[i].prototype;
60 // in this case we're extending with the prototype
61 parent = arguments[i];
63 OpenLayers.Util.extend(extended, parent);
65 Class.prototype = extended;
70 * Property: isPrototype
71 * *Deprecated*. This is no longer needed and will be removed at 3.0.
73 OpenLayers.Class.isPrototype = function () {};
76 * APIFunction: OpenLayers.create
77 * *Deprecated*. Old method to create an OpenLayers style class. Use the
78 * <OpenLayers.Class> constructor instead.
83 OpenLayers.Class.create = function() {
85 if (arguments && arguments[0] != OpenLayers.Class.isPrototype) {
86 this.initialize.apply(this, arguments);
93 * APIFunction: inherit
94 * *Deprecated*. Old method to inherit from one or more OpenLayers style
95 * classes. Use the <OpenLayers.Class> constructor instead.
98 * class - One or more classes can be provided as arguments
101 * An object prototype
103 OpenLayers.Class.inherit = function () {
104 var superClass = arguments[0];
105 var proto = new superClass(OpenLayers.Class.isPrototype);
106 for (var i=1, len=arguments.length; i<len; i++) {
107 if (typeof arguments[i] == "function") {
108 var mixin = arguments[i];
109 arguments[i] = new mixin(OpenLayers.Class.isPrototype);
111 OpenLayers.Util.extend(proto, arguments[i]);