Last active
August 29, 2015 14:12
-
-
Save seanstrom/831285122ec9058e0211 to your computer and use it in GitHub Desktop.
Exploring Constructors and Factories in Javascript - Wrapping Constructors With Object Example
This file contains hidden or 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 Person = require('./person') | |
var sean = Person.create('Sean Hagstrom', 20) | |
console.log(sean.name) // Sean Hagstrom | |
console.log(sean.age) // 20 |
This file contains hidden or 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 wrap = require('./wrap') | |
function Person(name, age) { | |
this.name = name | |
this.age = age | |
} | |
module.exports = wrap(Person) |
This file contains hidden or 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
function wrap(constructor) { | |
return { | |
create: function() { | |
var object = Object.create(constructor.prototype) | |
return constructor.apply(object, arguments) || object | |
} | |
} | |
} | |
module.exports = wrap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment