diff --git a/lib/OpenLayers/BaseTypes/Class.js b/lib/OpenLayers/BaseTypes/Class.js index 681c683ba9..81316236af 100644 --- a/lib/OpenLayers/BaseTypes/Class.js +++ b/lib/OpenLayers/BaseTypes/Class.js @@ -17,6 +17,8 @@ * To create a new OpenLayers-style class with multiple inheritance, use the * following syntax: * > var MyClass = OpenLayers.Class(Class1, Class2, prototype); + * Note that instanceof reflection will only reveil Class1 as superclass. + * Class2 ff are mixins. * */ OpenLayers.Class = function() { @@ -33,9 +35,21 @@ OpenLayers.Class = function() { } }; var extended = {}; - var parent; + var parent, initialize; for(var i=0, len=arguments.length; i 1) { + // replace the initialize method with an empty function, + // because we do not want to create a real instance here + initialize = arguments[i].prototype.initialize; + arguments[i].prototype.initialize = function() {}; + // the line below makes sure that the new class has a + // superclass + extended = new arguments[i]; + // restore the original initialize method + arguments[i].prototype.initialize = initialize; + } // get the prototype of the superclass parent = arguments[i].prototype; } else { diff --git a/tests/BaseTypes/Class.html b/tests/BaseTypes/Class.html index 30e934f954..47aea9fbc4 100644 --- a/tests/BaseTypes/Class.html +++ b/tests/BaseTypes/Class.html @@ -263,7 +263,22 @@ t.eq( objC.mixed, true, "class C has mixin properties" ); } + function test_Class_isInstanceOf(t) { + t.plan(7); + var wfs = new OpenLayers.Layer.WFS({}); + var drag = new OpenLayers.Control.DragFeature({}); + t.ok(wfs instanceof OpenLayers.Layer.WFS, "isInstanceOf(WFS)"); + t.ok(wfs instanceof OpenLayers.Layer, "isInstanceOf(Layer)"); + t.ok(!(wfs instanceof OpenLayers.Format), "not isInstanceOf(Format)"); + t.ok(drag instanceof OpenLayers.Control, "drag is a control"); + t.ok(!(drag instanceof OpenLayers.Layer), "drag is not a layer"); + //test a class with multiple inheritance + var BadClass=OpenLayers.Class(OpenLayers.Layer.WFS, OpenLayers.Control.DragFeature); + var bad = new BadClass({}); + t.ok(!(bad instanceof OpenLayers.Control), "bad is a control, but it is also a layer and we cannot have two superclasses"); + t.ok(bad instanceof OpenLayers.Layer, "bad is a layer, it inherits from the layer first"); + }