permit abstract classes (an abstract class being a class without an "initialize" method) as parent classes, final patch from tschaub, r=tschaub (closes #1987)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@9091 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -41,19 +41,17 @@ OpenLayers.Class = function() {
|
||||
// make the class passed as the first argument the superclass
|
||||
if(i == 0 && len > 1) {
|
||||
initialize = arguments[i].prototype.initialize;
|
||||
if(typeof initialize === "function") {
|
||||
// replace the initialize method with an empty function,
|
||||
// because we do not want to create a real instance here
|
||||
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;
|
||||
// replace the initialize method with an empty function,
|
||||
// because we do not want to create a real instance here
|
||||
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
|
||||
if(initialize === undefined) {
|
||||
delete arguments[i].prototype.initialize;
|
||||
} else {
|
||||
// if someone uses Class to mix objects, we don't want to
|
||||
// mess with the prototype
|
||||
extended = new arguments[i];
|
||||
arguments[i].prototype.initialize = initialize;
|
||||
}
|
||||
}
|
||||
// get the prototype of the superclass
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
}
|
||||
|
||||
function test_Class_inheritance(t) {
|
||||
t.plan(5);
|
||||
t.plan(7);
|
||||
|
||||
var BaseClass = OpenLayers.Class({
|
||||
prop: "base",
|
||||
@@ -84,6 +84,31 @@
|
||||
|
||||
var F = OpenLayers.Class(Object, {});
|
||||
t.ok(!("initialize" in Object.prototype), "no messing with non OL prototypes");
|
||||
|
||||
// test with an abstract class (i.e. a class that doesn't have an initialize
|
||||
// method) as the parent class
|
||||
var Vehicule = OpenLayers.Class({
|
||||
numWheels: null
|
||||
});
|
||||
var Bike = OpenLayers.Class(Vehicule, {
|
||||
initialize: function() {
|
||||
this.numWheels = 2;
|
||||
}
|
||||
});
|
||||
var b = new Bike();
|
||||
t.ok(b instanceof Vehicule, "a bike is a vehicule");
|
||||
|
||||
// test inheritance with something that has a non-function initialize property
|
||||
var P = OpenLayers.Class({
|
||||
initialize: "foo"
|
||||
});
|
||||
var C = OpenLayers.Class(P, {
|
||||
initialize: function() {
|
||||
// pass
|
||||
}
|
||||
});
|
||||
var c = new C();
|
||||
t.eq(P.prototype.initialize, "foo", "Class restores custom initialize property.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user