Created
June 9, 2022 11:43
-
-
Save ariesmcrae/1285d2cb354d2a05735f10315ea79fe4 to your computer and use it in GitHub Desktop.
node.js singleton example in typescript
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
class Logger { | |
public error(msg: string, exception: Error): void { | |
console.error(msg, exception); | |
} | |
} | |
const instance = new Logger(); | |
// Prevent modification to properties and values of the object. | |
Object.freeze(instance); | |
// node.js module based singleton | |
export { instance as MyLogger }; | |
// Usage: | |
// | |
// import { MyLogger } from './Logger'; | |
// MyLogger.error('Error message', new Error('My Error message')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment