Last active
September 30, 2015 06:17
-
-
Save softwarespot/a89217e7dd40c27c90fa to your computer and use it in GitHub Desktop.
Creating a module with interface coupled together using ES2015
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 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