Skip to content

Instantly share code, notes, and snippets.

@seanstrom
Last active August 29, 2015 14:11
Show Gist options
  • Save seanstrom/53ddbd60649fa6ddc4dc to your computer and use it in GitHub Desktop.
Save seanstrom/53ddbd60649fa6ddc4dc to your computer and use it in GitHub Desktop.
Exploring Constructors and Factories in Javascript - Constructor InstanceOf Guard Example
var Person = require('./person')
console.log(name) // undefined
console.log(age) // undefined
var sean = Person('Sean Hagstrom', 20)
console.log(sean.name) // Sean Hagstrom
console.log(sean.age) // 20
console.log(name) // undefined
console.log(age) // undefined
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