Skip to content

Instantly share code, notes, and snippets.

@torjusb
Created November 2, 2010 12:44
Show Gist options
  • Save torjusb/659559 to your computer and use it in GitHub Desktop.
Save torjusb/659559 to your computer and use it in GitHub Desktop.
// Create an inherhit / extend method
Function.prototype.inherits = function (class) {
$.extend(this.prototype, class.prototype);
}
var ExtendClass = function () {
// Call the "parent" constructor
Foobar.apply(this);
// Parent method
this.foo();
}
// Extended class' method
ExtendClass.prototype.superMethod = function () {
console.log('cake');
return this;
}
// Extend the Foobar class with the ExtendClass
ExtendClass.inherits(Foobar);
// Make it global
window.ExtendClass = ExtendClass;
( function (window, $) {
/**
** @ Constructor
**
** Function constructors should _always_ be capitalized
**/
var Foobar = function () {
// Properties
this.foop = 'bar';
};
/**
** @ Methods
**/
Foobar.prototype = (function () {
// Private methods
var privateFoo = function () {
console.log('private stuff');
},
privateBaz = function () {
return this;
}
// Public methods
return {
foo: function () {
// Private methods should be called using the apply method
privateFoo.apply(this);
return this;
},
baz: function () {
return this;
}
}
})();
/**
** @ Static methods
**/
Foobar.staticFoo = function () {
console.log('static foo');
return this;
}
Foobar.staticBaz = function () {
console.log('static baz');
return this;
}
/**
** @ Static properties
**/
Foobar.defaults = {
'foo': 'bar',
'baz': 'foo'
}
// Make it global
window.Foobar = Foobar;
/**
** @ jQuery plugin
**/
$.fn.foobar = function () {
return this.each( function () {
return new Foobar();
});
};
})(window, jQuery, undefined);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment