-
-
Save shobhitg/ef3376d3e0ecd76ba435bad48200e92b to your computer and use it in GitHub Desktop.
Create Singleton pattern using ES6
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
'use strict'; | |
/** | |
* Created by Milan Karunarathne | |
* Email: mhkarunarathne@gmail.com | |
* May be freely distributed under the MIT license | |
*/ | |
import EventEmitter from 'events'; | |
class Single extends EventEmitter { | |
constructor() { | |
this.name = 'Singleton'; | |
} | |
getName() { | |
return this.name; | |
} | |
setName(name) { | |
this.name = name; | |
} | |
} | |
export default let single = new Single(); |
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
'use strict'; | |
import Single from './ES6_Single'; | |
console.log(`Test one: ${Singleton.getName()}`); | |
console.log(Singleton.setName('New Single')); |
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
'use strict'; | |
import Single from './ES6_Single'; | |
setTimeout(() => { | |
console.log(`Test two: ${Singleton.getName()}`); | |
}, 500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment