Skip to content

Instantly share code, notes, and snippets.

@neolitec
Created July 24, 2011 15:03
Show Gist options
  • Save neolitec/1102707 to your computer and use it in GitHub Desktop.
Save neolitec/1102707 to your computer and use it in GitHub Desktop.
$.Class : MooTools-like class plugin for jQuery

$.Class : MooTools-like class for jQuery

This is a Mootools-like class implementation for jQuery in just 37 NCLOC.

The Big Picture

var Egg = new $.Class({
    initialize: function() { /* constructor */ },
});

Egg.extend({ // extend class prototype
    spam: function() { return 'holy mother of gosh'; }
});

var SuperEgg = new $.Class({
    Extends: Egg,
    spam: function() { return "spam spam spam"; }
});

var egg = new Egg();

egg.spam(); // 'holy mother of gosh'

egg.implement({ // aop style modifier
  monty: function() { return this; },
  python: function() { return this; }
});

egg.monty().python().spam();

var superEgg = new SuperEgg();

superEgg.spam();

superegg.implement({ // aop style modifier
  monty: function() { return this; },
  python: function() { return this; }
});

superegg.monty().python().spam();

Todo

  • method wrap support for protected and hidden methods.
  • Mutators (Implements)
  • hasOwnProperty checks.
/*!
* jQuery MooTools-like class plugin.
* @author Guillaume Coguiec <g.coguiec@gmail.com>
* @author Kevin Manson <kev.manson@gmail.com>
* Licence fall under Mootools's.
*/
(function($, window) {
var enumerables = true, slice = Array.prototype.slice, has = Object.prototype.hasOwnProperty;
for (var i in { toString: 1 }) { enumerables = null; }
enumerables = (enumerables) ? [ 'hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable',
'toLocaleString', 'toString', 'constructor' ] : enumerables;
window.Function.prototype.overload = function() {
var e = enumerables, self = this;
return function() {
var argc = arguments.length, argv = slice.call(arguments);
if (0 === argc) { return this; }
if ('string' === typeof argv[0]) { self.call(this, argv[0], argv[1]); return this; }
for (var o in argv) for (var k in argv[o]) {
self.call(this, k, argv[o][k]);
if (e) for (var po in e) if (has.call(argv[o], e[po])) { self.call(this, e[po], this[e[po]]); }
}
return this;
};
};
$.Class = function(params) {
if ('undefined' === typeof params) { return $.noop; }
params = ($.isFunction(params)) ? { initialize: params } : params;
this['$instance'] = function() { return (this.initialize) ? this.initialize.apply(this, arguments) : this; };
var $instance = this['$instance']; // hack to prevent chrome inspector lexer to display internal var...
delete this['$instance'];
$instance.extend = function(key, value) { this.prototype[key] = value; }.overload();
$instance.prototype.implement = function(key, value) { this[key] = value; }.overload();
$.extend($instance, this);
if(has.call(params, "Extends")) {
if($.isFunction(params.Extends)) $instance.extend(params.Extends.prototype);
else if($.isArray(params.Extends))
for(e in params.Extends) $instance.extend(params.Extends[e].prototype);
}
delete $instance.prototype.Extends;
$.extend($instance.prototype, params);
return $instance;
};
})(jQuery, this);
/*!
* jQuery MooTools-like class plugin.
* @author Guillaume Coguiec <g.coguiec@gmail.com>
* @author Kevin Manson <kev.manosn@gmail.com>
* Licence fall under Mootools's.
*/
(function($,window){var enumerables=true,slice=Array.prototype.slice,has=Object.prototype.hasOwnProperty;for(var i in{toString:1}){enumerables=null;}
enumerables=(enumerables)?['hasOwnProperty','valueOf','isPrototypeOf','propertyIsEnumerable','toLocaleString','toString','constructor']:enumerables;window.Function.prototype.overload=function(){var e=enumerables,self=this;return function(){var argc=arguments.length,argv=slice.call(arguments);if(0===argc){return this;}
if('string'===typeof argv[0]){self.call(this,argv[0],argv[1]);return this;}
for(var o in argv)for(var k in argv[o]){self.call(this,k,argv[o][k]);if(e)for(var po in e)if(has.call(argv[o],e[po])){self.call(this,e[po],this[e[po]]);}}
return this;};};$.Class=function(params){if('undefined'===typeof params){return $.noop;}
params=($.isFunction(params))?{initialize:params}:params;this['$instance']=function(){return(this.initialize)?this.initialize.apply(this,arguments):this;};var $instance=this['$instance'];delete this['$instance'];$instance.extend=function(key,value){this.prototype[key]=value;}.overload();$instance.prototype.implement=function(key,value){this[key]=value;}.overload();$.extend($instance,this);if(has.call(params,"Extends")){if($.isFunction(params.Extends))$instance.extend(params.Extends.prototype);else if($.isArray(params.Extends))
for(e in params.Extends)$instance.extend(params.Extends[e].prototype);}
delete $instance.prototype.Extends;$.extend($instance.prototype,params);return $instance;};})(jQuery,this);
@neolitec
Copy link
Author

Finally, the "Extends" syntax isn't so bad...

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