Created
June 30, 2010 20:14
-
-
Save ryedin/459158 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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