Skip to content

Instantly share code, notes, and snippets.

@jubstuff
Created October 9, 2011 10:43
Show Gist options
  • Save jubstuff/1273542 to your computer and use it in GitHub Desktop.
Save jubstuff/1273542 to your computer and use it in GitHub Desktop.
/* class implementation for javascript */
var Class = function(parent){
var klass = function(){
// call a function with a given _this_ argument and
// an array of arguments
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply
this.init.apply(this, arguments);
};
if (parent) {
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
}
klass.prototype.init = function(){};
//Shortcut to access prototype
klass.fn = klass.prototype;
klass.fn.parent = klass;
klass._super = klass.__proto__;
//shortcut to access class
klass.fn.parent = klass;
// Adding class properties (static members)
klass.extend = function(obj) {
/* aggiunge una funzione di callback
* chiamata extended: se è presente,
* all'atto dell'inserimento delle
* funzioni, questa funzione viene
* richiamata
*/
var extended = obj.extended;
for(var i in obj){
klass[i] = obj[i];
}
//callback function
if (extended) extended(klass);
};
// Adding instance properties
klass.include = function(obj){
var included = obj.included;
for(var i in obj){
klass.fn[i] = obj[i];
}
//callback function
if (included) included(klass);
};
return klass;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment