Created
January 10, 2013 07:39
-
-
Save klebervirgilio/4500224 to your computer and use it in GitHub Desktop.
JS Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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