Created
February 1, 2018 10:46
-
-
Save joshuacerbito/51107829d322855837cc927e63990c6f to your computer and use it in GitHub Desktop.
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
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