Skip to content

Instantly share code, notes, and snippets.

@seanstrom
Created December 23, 2014 04:51
Show Gist options
  • Save seanstrom/792c6af6253bd49892e8 to your computer and use it in GitHub Desktop.
Save seanstrom/792c6af6253bd49892e8 to your computer and use it in GitHub Desktop.
Exploring Constructors and Factories in Javascript - Constructor/Factory Example
var createPerson = require('./person')
var sean = createPerson('Sean Hagstrom', 20)
console.log(sean.name) // Sean Hagstrom
console.log(sean.age) // 20
function Person(name, age) {
if(!(this instanceof Person)) {
return new Person(name, age)
}
this.name = name
this.age = age
}
module.exports = Person
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment