Skip to content

Instantly share code, notes, and snippets.

@seanmonstar
Created May 9, 2011 18:01
Show Gist options
  • Save seanmonstar/962996 to your computer and use it in GitHub Desktop.
Save seanmonstar/962996 to your computer and use it in GitHub Desktop.
var extend = function(child, parent) {
for(var i in parent) {
if(parent.hasOwnProperty(i)) {
child[i] = parent[i];
}
}
return child;
};
var mutate = function(child, parent) {
for(var m in Class.Mutators) if (Class.Mutators.hasOwnProperty(m)) {
if (parent.hasOwnProperty(m)) {
Class.Mutators[m].call(child, parent[m]);
delete child[m];
}
}
return extend(child.prototype, parent);
};
var blueprint = function(klass) {
klass.$prototyping = true;
var proto = new klass;
delete klass.$prototyping;
return proto;
};
var Class = function(params) {
var klass = extend(function() {
if (klass.$prototype) return this;
return this.initialize ? this.initialize.apply(this, arguments) : this;
}, this);
mutate(klass, params);
klass.prototype.constructor = klass;
return klass;
}
var toString = Object.prototype.toString;
var isArray = function(obj) {
return toString.call(obj) == '[object Array]';
};
Class.Mutators = {
Extends: function(parent) {
this.prototype = blueprint(parent);
},
Implements: function(mixins) {
mixins = isArray(mixis) ? mixins : [mixins];
for(var i = 0, len = mixins.length; i < len; i++) {
extend(this, blueprint(mixins[i]));
}
}
}
module.exports = Class;
@subtleGradient
Copy link

this looks pretty good. Does it pass all the Class specs?

@seanmonstar
Copy link
Author

haven't had a chance to try yet. this was part of a prototype I was making that wanted actual modules of a couple base Moo classes, so you can truly require('class')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment