Skip to content

Instantly share code, notes, and snippets.

@softwarespot
Last active September 30, 2015 06:17
Show Gist options
  • Save softwarespot/a89217e7dd40c27c90fa to your computer and use it in GitHub Desktop.
Save softwarespot/a89217e7dd40c27c90fa to your computer and use it in GitHub Desktop.
Creating a module with interface coupled together using ES2015
const myModule = ((interface) => {
// Implementing the interface
// This is like an interface, which doesn't pollute the main module with unrelated code
console.log(interface.init());
// Instantiate a new class that is attached to the interface
const _printClass = new interface.printClass();
return {
// Initialise the outer module
init: () => {
return 'Outer module function (Public Module)';
},
// Print a value using the 'printClass' object reference
print: (value) => {
_printClass.print(value);
},
// Expose the underlying interface
getInterface: () => {
return interface;
}
};
})(((config) => {
// Interface
// Displaying the passed parameter data
console.log(config);
return {
init: function () {
return 'Inner module function (Interface)';
},
printClass: class {
constructor() {
// Empty
}
print(value) {
console.log(value);
}
}
};
})('Passed to inner module (Interface)'));
myModule.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment