Last active
June 25, 2018 11:57
-
-
Save oinak/26230d6df70721cb525e9a828d2a6dc2 to your computer and use it in GitHub Desktop.
Factory Module pattern in modren js
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
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