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

View File

@@ -23,49 +23,22 @@
*
*/
OpenLayers.Class = function() {
var Class = function() {
/**
* This following condition can be removed at 3.0 - this is only for
* backwards compatibility while the Class.inherit method is still
* in use. So at 3.0, the following three lines would be replaced with
* simply:
* this.initialize.apply(this, arguments);
*/
if (arguments && arguments[0] != OpenLayers.Class.isPrototype) {
this.initialize.apply(this, arguments);
}
};
var extended = {};
var parent, initialize, Type;
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);
var len = arguments.length;
var P = arguments[0];
var F = arguments[len-1];
var C = typeof F.initialize == "function" ?
F.initialize :
function(){ P.apply(this, arguments); };
if (len > 1) {
var newArgs = [C, P].concat(
Array.prototype.slice.call(arguments).slice(1, len-1), F);
OpenLayers.inherit.apply(null, newArgs);
} else {
C.prototype = F;
}
Class.prototype = extended;
return Class;
return C;
};
/**
@@ -90,7 +63,6 @@ OpenLayers.Class.create = function() {
};
};
/**
* APIFunction: inherit
* *Deprecated*. Old method to inherit from one or more OpenLayers style
@@ -102,15 +74,35 @@ OpenLayers.Class.create = function() {
* Returns:
* An object prototype
*/
OpenLayers.Class.inherit = function () {
var superClass = arguments[0];
var proto = new superClass(OpenLayers.Class.isPrototype);
for (var i=1, len=arguments.length; i<len; i++) {
if (typeof arguments[i] == "function") {
var mixin = arguments[i];
arguments[i] = new mixin(OpenLayers.Class.isPrototype);
}
OpenLayers.Util.extend(proto, arguments[i]);
}
return proto;
OpenLayers.Class.inherit = function (P) {
var C = function() {
P.call(this);
};
var newArgs = [C].concat(Array.prototype.slice.call(arguments));
OpenLayers.inherit.apply(null, newArgs);
return C.prototype;
};
/**
* 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);
}
};