Skip to content

Instantly share code, notes, and snippets.

@colbyr
Created October 3, 2012 04:48
Show Gist options
  • Save colbyr/3825058 to your computer and use it in GitHub Desktop.
Save colbyr/3825058 to your computer and use it in GitHub Desktop.
Make a class in a module
/**
* use underscore js because its useful
*/
var _ = require('underscore');
/**
* Constructor -- `new Class('frosted')`
*/
function Class(prop) {
this.prop = prop;
}
/**
* this method won't be visible outside this file
*/
function _privateMethod(param) {
return param + ' butts';
}
/**
* add some instance methods to the prototype
*/
_.extend(Class.prototype, {
/**
* accessible via `new Class('frosted').method()`
*/
method: function () {
return _privateMethod(this.prop);
}
});
/**
* add some static methods to the class
*/
_.extend(Class, {
/**
* accessible via `Class.static_method()`
*/
static_method: function (param) {
return _privateMethod(param);
}
});
/**
* export the constructor function
*/
exports = Class;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment