slight class refactoring

This commit is contained in:
mourner 2010-09-02 17:49:15 +03:00
parent 34305dabe0
commit 763b01ef08

View File

@ -4,45 +4,47 @@
L.Class = function() {}; L.Class = function() {};
//Thanks to John Resig and Dean Edwards for inspiration
L.Class.extend = function(props) { L.Class.extend = function(props) {
var _super = this.prototype, statics; // extended class with the new prototype
function NewClass() {
if (!L.Class._prototyping && this.initialize) {
this.initialize.apply(this, arguments);
}
}
// instantiate class without calling constructor // instantiate class without calling constructor
L.Class._prototyping = true; L.Class._prototyping = true;
var proto = new this(); var proto = new this();
L.Class._prototyping = false; L.Class._prototyping = false;
proto.constructor = NewClass;
NewClass.prototype = proto;
// add callParent method
if (this != L.Class) {
var _super = this.prototype;
proto.callParent = function(fnName) {
_super[fnName].apply(this, Array.prototype.slice.call(arguments, 1));
};
}
// mix static properties into the class
if (props.statics) {
L.Util.extend(NewClass, props.statics);
delete props.statics;
}
// mix includes into the prototype // mix includes into the prototype
if (props.includes) { if (props.includes) {
L.Util.extend.apply(null, [proto].concat(props.includes)); L.Util.extend.apply(null, [proto].concat(props.includes));
delete props.includes; delete props.includes;
} }
// callParent method
if (this != L.Class) {
proto.callParent = function(fnName) {
_super[fnName].apply(this, Array.prototype.slice.call(arguments, 1));
};
}
// save static properties
if (props.statics) {
statics = props.statics;
delete props.statics;
}
// mix given properties into the prototype // mix given properties into the prototype
L.Util.extend(proto, props); L.Util.extend(proto, props);
// extended class with the new prototype // allow inheriting further
function NewClass() {
if (!L.Class._prototyping && this.initialize) {
this.initialize.apply(this, arguments);
}
}
proto.constructor = NewClass;
NewClass.prototype = proto;
NewClass.extend = arguments.callee; NewClass.extend = arguments.callee;
// method for adding properties to prototype // method for adding properties to prototype
@ -50,11 +52,6 @@ L.Class.extend = function(props) {
L.Util.extend(this.prototype, props); L.Util.extend(this.prototype, props);
}; };
// mix static properties into the class
if (statics) {
L.Util.extend(NewClass, statics);
}
//inherit parent's statics //inherit parent's statics
for (var i in this) { for (var i in this) {
if (this.hasOwnProperty(i) && i != 'prototype') { if (this.hasOwnProperty(i) && i != 'prototype') {