Made instanceof reflection work for class hierarchies. r=elemoine,pvalsecc (closes #1802)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@8445 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -17,6 +17,8 @@
|
|||||||
* To create a new OpenLayers-style class with multiple inheritance, use the
|
* To create a new OpenLayers-style class with multiple inheritance, use the
|
||||||
* following syntax:
|
* following syntax:
|
||||||
* > var MyClass = OpenLayers.Class(Class1, Class2, prototype);
|
* > var MyClass = OpenLayers.Class(Class1, Class2, prototype);
|
||||||
|
* Note that instanceof reflection will only reveil Class1 as superclass.
|
||||||
|
* Class2 ff are mixins.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
OpenLayers.Class = function() {
|
OpenLayers.Class = function() {
|
||||||
@@ -33,9 +35,21 @@ OpenLayers.Class = function() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
var extended = {};
|
var extended = {};
|
||||||
var parent;
|
var parent, initialize;
|
||||||
for(var i=0, len=arguments.length; i<len; ++i) {
|
for(var i=0, len=arguments.length; i<len; ++i) {
|
||||||
if(typeof arguments[i] == "function") {
|
if(typeof arguments[i] == "function") {
|
||||||
|
// make the class passed as the first argument the superclass
|
||||||
|
if(i == 0 && len > 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
|
// get the prototype of the superclass
|
||||||
parent = arguments[i].prototype;
|
parent = arguments[i].prototype;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -263,7 +263,22 @@
|
|||||||
t.eq( objC.mixed, true, "class C has mixin properties" );
|
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");
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user