Skip to content

Instantly share code, notes, and snippets.

@joshuacerbito
Created February 1, 2018 10:46
Show Gist options
  • Save joshuacerbito/51107829d322855837cc927e63990c6f to your computer and use it in GitHub Desktop.
Save joshuacerbito/51107829d322855837cc927e63990c6f to your computer and use it in GitHub Desktop.
const ObjectSingleton = {
_instance: null,
get instance() {
if (!this._instance) {
this._instance = {
singletonMethod() {
return 'singletonMethod';
},
_type: 'ObjectSingleton',
get type() {
return this._type;
},
set type(value) {
this._type = value;
}
};
}
return this._instance;
}
};
export default ObjectSingleton;
// Flux store way
export const ObjectSingletonAssign = Object.assign({}, {
singletonMethod() {
return 'singletonMethod';
},
_type: 'ObjectSingletonAssign',
get type() {
return this._type;
},
set type(value) {
this._type = value;
}
});
// index.js
import ObjectSingleton, { ObjectSingletonAssign } from './ObjectSingleton';
// Instance
const instance = ObjectSingleton.instance;
// Prototype Method
console.log(instance.type, instance.singletonMethod());
// Getter/Setter
instance.type = 'type updated';
console.log(instance.type);
/*
Flux
*/
// Prototype Method
console.log(ObjectSingletonAssign.type, ObjectSingletonAssign.singletonMethod());
// Getter/Setter
ObjectSingletonAssign.type = 'type updated';
console.log(ObjectSingletonAssign.type);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment