Skip to content

Instantly share code, notes, and snippets.

@MorganConrad
Created November 15, 2013 17:02
Show Gist options
  • Save MorganConrad/7487819 to your computer and use it in GitHub Desktop.
Save MorganConrad/7487819 to your computer and use it in GitHub Desktop.
Example of "classical" style JavaScript
module.exports = Foo; // just one line to get all of this code.
// Now how much would you pay?
/** constructor capital first letter to remind callers**/
function Foo(args) {
if (!this instanceof Foo) // but callers do screw up...
return new Foo(args) // or, if you wish, throw something
this.useSoap = args.useSoap ; // define instance vars here
this.bar = {};
}
// equivalent to static constants (ideally should be final)
Foo.CONSTANT1 = 'specialConstant';
// static function, no "this"
Foo.function1 = function(args) {
...
}
// method, "this" is almost certainly a Foo
Foo.prototype.method1 = function(args) {
...
};
// "hide" private method by convention of leading underscore
Foo.prototype._privatemethod1 = function(args) {
...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment