Created
February 19, 2012 11:42
-
-
Save Shamaoke/1863377 to your computer and use it in GitHub Desktop.
Using module.require in Node.js
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
// ross.js | |
this.ross = { | |
name: 'Ross', | |
surname: 'Geller', | |
get_name: function() { return this.name }, | |
get_surname: function() { return this.surname }, | |
get_fullname: function() { return this.name + ' ' + this.surname } | |
} | |
this.module = module | |
// chandler_joey.js | |
this.chandler = Object.create(module.parent.exports.ross) // module.parent will be "ross" if the current file is required | |
// by the "ross" module in "friends" through module.require | |
this.chandler.name = 'Chandler' | |
this.chandler.surname = 'Bing' | |
this.joey = Object.create(module.parent.exports.ross) // same as in "chandler" | |
this.joey.name = 'Joey' | |
this.joey.surname = 'Tibbiani' | |
// friends.js | |
;(function(one) { | |
for (name in one) console.log(one[name].get_fullname()) | |
})(require('./ross').module.require('./chandler_joey')) // the actual usage | |
// usage: | |
// $ node friends.js //=> Chandler Bing | |
// Joey Tribbiani |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment