Skip to content

Instantly share code, notes, and snippets.

@addyosmani
Forked from comfuture/jquery.oop.js
Created November 30, 2010 06:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addyosmani/721242 to your computer and use it in GitHub Desktop.
Save addyosmani/721242 to your computer and use it in GitHub Desktop.
(function($){
$.Class = function(code){
var klass = function() {
var instance = (arguments[0] !== null && this.__init__ && typeof this.__init__ == 'function') ? this.__init__.apply(this, arguments) : this;
return instance;
};
$.extend(klass, this);
$.extend(klass.prototype, {
'bind': function(type, fn) {
this.__events__ = this.__events__ || {};
this.__events__[type] = this.__events__[type] || [];
this.__events__[type].push(fn);
return this;
},
'unbind': function(type, fn) {
if (this.__events__ && this.__events__[type]) {
this.__events__[type].splice(this.__events__[type].indexOf(fn), 1);
}
return this;
},
'trigger': function(type, args){
if (this.__events__ && this.__events__[type]){
var evt = $.Event(type);
evt.target = this;
$.each(this.__events__[type], function(idx, fn) {
fn.apply(this, $.merge([evt], args || []));
});
}
return this;
}
}, code);
klass.constructor = $.Class;
return klass;
};
var merge = function(previous, current){
if (previous && previous != current){
var type = typeof current;
if (type != typeof previous) return current;
switch(type){
case 'function':
var merged = function(){
this.parent = arguments.callee.parent;
return current.apply(this, arguments);
};
merged.parent = previous;
return merged;
case 'object': return $.merge(previous, current);
}
}
return current;
};
$.extend(true, $.Class.prototype, {
'extend': function(properties) {
var proto = new this(null);
for (var property in properties){
var pp = proto[property];
proto[property] = merge(pp, properties[property]);
}
return new $.Class(proto);
},
'implement': function() {
for (var i = 0, l = arguments.length; i < l; i++) $.extend(this.prototype, arguments[i]);
}
});
// convenient shortcut
window.Class = function(code) {
return new $.Class(code);
};
})(jQuery);
var Person = Class({
__init__: function(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
});
var Animal = Class({
__init__: function(type) {
this.type = type;
},
bark: function() {
alert('...');
}
});
var Cat = Animal.extend({
__init__: function() {
this.parent('cat');
}
});
var Dog = Animal.extend({
__init__: function() {
this.parent('dog');
},
bark: function() {
alert('bow wow!');
}
});
// Person will act like class
var p1 = new Person('maroo', 31, 'M');
// Cat and Dog also works fine
var cat = new Cat();
var dog = new Dog();
dog.bark(); // will produce 'bow wow!' alert
// class can listen for custom event
cat.bind('myevent', function(evt) {
alert(evt.type + ': target:' + evt.target.name + ' myevent fired!');
});
// class can fire custom event
cat.trigger('myevent');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment