Skip to content

Commit

Permalink
insuring implement is removed on initialization, adding remove functi…
Browse files Browse the repository at this point in the history
…on to unsure correct removal process
  • Loading branch information
Angelo Dini committed May 16, 2012
1 parent 382d5c2 commit b7751cb
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/class.js
Expand Up @@ -17,19 +17,19 @@
obj = obj || {};
// call initialize if given
CONSTRUCTOR = function () {
var self = window === this ? copy(CONSTRUCTOR.prototype) : this;
return (this.initialize) ? this.initialize.apply(this, arguments) : self;
};
// adds implement to the class itself
if(obj.implement) {
obj = extend(obj, implement(obj.implement));
obj.implement = null;
var self = window === this ? copy(CONSTRUCTOR.prototype) : this;
var imp = obj.implement;
delete obj.implement;
remove(obj, 'implement');
obj = extend(obj, implement(imp));
}
// assign prototypes
CONSTRUCTOR.prototype = copy(obj);
// assign correct constructor
CONSTRUCTOR.constructor = CONSTRUCTOR;
// save initial object as parent so it can be called by this.parent
CONSTRUCTOR._parent = copy(obj);
// attaching class properties to constructor
Expand All @@ -47,7 +47,7 @@
if(obj.implement) {
this.prototype = extend(this.prototype, implement(obj.implement));
// remove implement from obj
delete obj.implement;
remove(obj, 'implement');
}
// check if we should invoke parent when its called within a method
for(var key in obj) {
Expand Down Expand Up @@ -98,6 +98,20 @@
return new F();
}

// insures the removal of a given method name
function remove(obj , name, safe){
// if save is active we need to copy all attributes over.
if(safe) {
var safeObj = {};
for(var key in obj) {
if(key !== name) safeObj[key] = obj[key];
}
} else {
delete obj[name];
}
return safeObj || obj;
}

// helper for merging two object with each other
function extend(oldObj, newObj, preserve) {
// failsave if something goes wrong
Expand Down Expand Up @@ -128,11 +142,14 @@
// check if a class is implemented and save its prototype
if(typeof(array[i]) === 'function') array[i] = array[i].prototype;

// safely remove initialize
var safe = remove(array[i], 'initialize', true);

// we use implement again if array has the apropriate methiod, otherwise we extend
if(array[i].implement) {
collection = implement(array[i].implement);
if(safe.implement) {
collection = implement(safe.implement);
} else {
collection = extend(collection, array[i].prototype || array[i]);
collection = extend(collection, safe);
}
}

Expand Down

0 comments on commit b7751cb

Please sign in to comment.