Remove overhead from OpenLayers.Class, using erilem's fancy OpenLayers.inherit function. r=erilem (closes #2899)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10858 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2010-10-22 20:34:21 +00:00
parent 1b44b82908
commit e087e31621
+46 -54
View File
@@ -23,49 +23,22 @@
* *
*/ */
OpenLayers.Class = function() { OpenLayers.Class = function() {
var Class = function() { var len = arguments.length;
/** var P = arguments[0];
* This following condition can be removed at 3.0 - this is only for var F = arguments[len-1];
* backwards compatibility while the Class.inherit method is still
* in use. So at 3.0, the following three lines would be replaced with var C = typeof F.initialize == "function" ?
* simply: F.initialize :
* this.initialize.apply(this, arguments); function(){ P.apply(this, arguments); };
*/
if (arguments && arguments[0] != OpenLayers.Class.isPrototype) { if (len > 1) {
this.initialize.apply(this, arguments); var newArgs = [C, P].concat(
} Array.prototype.slice.call(arguments).slice(1, len-1), F);
}; OpenLayers.inherit.apply(null, newArgs);
var extended = {}; } else {
var parent, initialize, Type; C.prototype = F;
for(var i=0, len=arguments.length; i<len; ++i) {
Type = arguments[i];
if(typeof Type == "function") {
// make the class passed as the first argument the superclass
if(i == 0 && len > 1) {
initialize = Type.prototype.initialize;
// replace the initialize method with an empty function,
// because we do not want to create a real instance here
Type.prototype.initialize = function() {};
// the line below makes sure that the new class has a
// superclass
extended = new Type();
// restore the original initialize method
if(initialize === undefined) {
delete Type.prototype.initialize;
} else {
Type.prototype.initialize = initialize;
}
}
// get the prototype of the superclass
parent = Type.prototype;
} else {
// in this case we're extending with the prototype
parent = Type;
}
OpenLayers.Util.extend(extended, parent);
} }
Class.prototype = extended; return C;
return Class;
}; };
/** /**
@@ -90,7 +63,6 @@ OpenLayers.Class.create = function() {
}; };
}; };
/** /**
* APIFunction: inherit * APIFunction: inherit
* *Deprecated*. Old method to inherit from one or more OpenLayers style * *Deprecated*. Old method to inherit from one or more OpenLayers style
@@ -102,15 +74,35 @@ OpenLayers.Class.create = function() {
* Returns: * Returns:
* An object prototype * An object prototype
*/ */
OpenLayers.Class.inherit = function () { OpenLayers.Class.inherit = function (P) {
var superClass = arguments[0]; var C = function() {
var proto = new superClass(OpenLayers.Class.isPrototype); P.call(this);
for (var i=1, len=arguments.length; i<len; i++) { };
if (typeof arguments[i] == "function") { var newArgs = [C].concat(Array.prototype.slice.call(arguments));
var mixin = arguments[i]; OpenLayers.inherit.apply(null, newArgs);
arguments[i] = new mixin(OpenLayers.Class.isPrototype); return C.prototype;
} };
OpenLayers.Util.extend(proto, arguments[i]);
} /**
return proto; * Function: OpenLayers.inherit
*
* Parameters:
* C - {Object} the class that inherits
* P - {Object} the superclass to inherit from
*
* In addition to the mandatory C and P parameters, an arbitrary number of
* objects can be passed, which will extend C.
*/
OpenLayers.inherit = function(C, P) {
var F = function() {};
F.prototype = P.prototype;
C.prototype = new F;
var i, l, o;
for(i=2, l=arguments.length; i<l; i++) {
o = arguments[i];
if(typeof o === "function") {
o = o.prototype;
}
OpenLayers.Util.extend(C.prototype, o);
}
}; };