Skip to content

Instantly share code, notes, and snippets.

@oinak
Last active June 25, 2018 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oinak/26230d6df70721cb525e9a828d2a6dc2 to your computer and use it in GitHub Desktop.
Save oinak/26230d6df70721cb525e9a828d2a6dc2 to your computer and use it in GitHub Desktop.
Factory Module pattern in modren js
window.Namespace = window.Namespace || {};
// Usage:
// Namespace.Rolenamee.init({ prop1: '.prop1', prop2: '.prop2' })
const Rolename = (function Rolename(expose) {
// private class/constructor
function Implementor({ foo, bar }) {
this.foo = $(foo); // store a value that will be needed asyncronously
$(bar).on("event", () => { // set up an asyncronous reaction
this.foo.method(); // use the stored value asyncronously through =>'s lexical `this`
});
}
// Return a new Implementor instance bound to a particular `foo` and `bar` values
expose.init = function init({ foo, bar }) {
return new Implementor({ foo, bar });
};
return expose;
})({});
window.Namespace.Rolename = Rolename;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment