Skip to content

Instantly share code, notes, and snippets.

@ryedin
Created June 30, 2010 20:14
Show Gist options
  • Save ryedin/459158 to your computer and use it in GitHub Desktop.
Save ryedin/459158 to your computer and use it in GitHub Desktop.
var sys = require('sys');
//use protoype methods for massive speed improvement (and less memory consumption)
function greetBehaviors(message) {
this.message = message;
}
greetBehaviors.prototype.greet = function() {
sys.puts(this.message);
};
greetBehaviors.prototype.slowGreet = function() {
sys.puts(this.message);
};
// Define the factory (encapsulate private data while still using a single memory location for the functions)
function newPerson(name, age) {
// Store the message in a closure
var message = name + ", who is " + age + " years old, says hi!";
return new greetBehaviors(message);
}
// Export the factory as a module, keeping a) the methods shared across instances and b) the actual "class" private to this file (in CommonJS land anyway)
module.exports = newPerson;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment