Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ES6 singleton pattern: module default exports an instance
class SingletonDefaultExportInstance {
constructor() {
this._type = 'SingletonDefaultExportInstance';
singletonMethod() {
return 'singletonMethod';
static staticMethod() {
return 'staticMethod';
get type() {
return this._type;
set type(value) {
this._type = value;
export default new SingletonDefaultExportInstance();
// ...
// index.js
import SingletonDefaultExportInstance from './SingletonDefaultExportInstance';
// Instantiate
// console.log(new SingletonDefaultExportInstance); // is not a constructor
// Prototype Method
console.log(SingletonDefaultExportInstance.type, SingletonDefaultExportInstance.singletonMethod());
// Getter/Setter
SingletonDefaultExportInstance.type = 'type updated';
// Static method
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.