From 22af9d6412f3ea5bddcb598404da2b5d339eff69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 19 Mar 2009 14:29:12 +0000 Subject: [PATCH] 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 --- lib/OpenLayers/BaseTypes/Class.js | 22 ++++++++++------------ tests/BaseTypes/Class.html | 27 ++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Class.js b/lib/OpenLayers/BaseTypes/Class.js index d9f1ad5551..0f21c37a72 100644 --- a/lib/OpenLayers/BaseTypes/Class.js +++ b/lib/OpenLayers/BaseTypes/Class.js @@ -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 diff --git a/tests/BaseTypes/Class.html b/tests/BaseTypes/Class.html index ccb155ca6f..bcf827e80f 100644 --- a/tests/BaseTypes/Class.html +++ b/tests/BaseTypes/Class.html @@ -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."); }