Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Singleton pattern using ES6
'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();
'use strict';
import Single from './ES6_Single';
console.log(`Test one: ${Singleton.getName()}`);
console.log(Singleton.setName('New Single'));
'use strict';
import Single from './ES6_Single';
setTimeout(() => {
console.log(`Test two: ${Singleton.getName()}`);
}, 500);
@allencoded

This comment has been minimized.

Copy link

allencoded commented Oct 17, 2016

Tried the same approach you have, but I get an error with unexpected token let:
export default let single = new Single();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.