Skip to content

Instantly share code, notes, and snippets.

@klebervirgilio
Created January 10, 2013 07:39
Show Gist options
  • Save klebervirgilio/4500224 to your computer and use it in GitHub Desktop.
Save klebervirgilio/4500224 to your computer and use it in GitHub Desktop.
JS Class
var Class = function(parent){
var klass = function(){
this.init.apply(this, arguments);
}
if(parent){
var subclass = function(){};
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
}
klass.prototype.init = function(){};
klass.fn = klass.prototype;
klass.fn.parent = klass;
klass._super = klass.__proto__;
// Adding class properties
klass.extend = function(obj){
var extended = obj.extended;
for(var i in obj){
klass[i] = obj[i];
}
if(extended) extended(klass);
}
// Adding class properties
klass.include = function(obj){
var included = obj.included;
for(var i in obj){
klass.fn[i] = obj[i];
}
if(included) included(klass);
}
return klass;
}
var Person = new Class();
Person.extend({
find: function(){ return console.log("find:", arguments);}
});
Person.include({
save: function(){ return console.log("save:", arguments);}
});
Person.find(1)
new Person().save({validate: false})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment