-
-
Save larrybolt/587cfaf51f4bc5b618cd0308f3114ea3 to your computer and use it in GitHub Desktop.
NodeJS require singletons
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
const common = require('./common') | |
module.exports.add = (e) => common.add(e) | |
module.exports.get = () => common.get() |
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
const common = require('./common') | |
module.exports.add = (e) => common.add(e) | |
module.exports.get = () => common.get() |
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
const c = []; | |
module.exports.add = (n) => c.push(n) | |
module.exports.get = () => c |
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
// run this file with node | |
// require both a and b, | |
const a = require('./a'); | |
const b = require('./b'); | |
// both a and b require common, | |
a.add('a') | |
b.add('b') | |
console.log('a', a.get()) | |
console.log('b', b.get()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment