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
|
// make the class passed as the first argument the superclass
|
||||||
if(i == 0 && len > 1) {
|
if(i == 0 && len > 1) {
|
||||||
initialize = arguments[i].prototype.initialize;
|
initialize = arguments[i].prototype.initialize;
|
||||||
if(typeof initialize === "function") {
|
// replace the initialize method with an empty function,
|
||||||
// replace the initialize method with an empty function,
|
// because we do not want to create a real instance here
|
||||||
// because we do not want to create a real instance here
|
arguments[i].prototype.initialize = function() {};
|
||||||
arguments[i].prototype.initialize = function() {};
|
// the line below makes sure that the new class has a
|
||||||
// the line below makes sure that the new class has a
|
// superclass
|
||||||
// superclass
|
extended = new arguments[i];
|
||||||
extended = new arguments[i];
|
// restore the original initialize method
|
||||||
// restore the original initialize method
|
if(initialize === undefined) {
|
||||||
arguments[i].prototype.initialize = initialize;
|
delete arguments[i].prototype.initialize;
|
||||||
} else {
|
} else {
|
||||||
// if someone uses Class to mix objects, we don't want to
|
arguments[i].prototype.initialize = initialize;
|
||||||
// mess with the prototype
|
|
||||||
extended = new arguments[i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// get the prototype of the superclass
|
// get the prototype of the superclass
|
||||||
|
|||||||
@@ -51,7 +51,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_Class_inheritance(t) {
|
function test_Class_inheritance(t) {
|
||||||
t.plan(5);
|
t.plan(7);
|
||||||
|
|
||||||
var BaseClass = OpenLayers.Class({
|
var BaseClass = OpenLayers.Class({
|
||||||
prop: "base",
|
prop: "base",
|
||||||
@@ -85,6 +85,31 @@
|
|||||||
var F = OpenLayers.Class(Object, {});
|
var F = OpenLayers.Class(Object, {});
|
||||||
t.ok(!("initialize" in Object.prototype), "no messing with non OL prototypes");
|
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.");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_Class_multiple_inheritance(t) {
|
function test_Class_multiple_inheritance(t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user