Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Last active April 15, 2020 01:44
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 matthewstokeley/69d47061d9307d440127d32751429a62 to your computer and use it in GitHub Desktop.
Save matthewstokeley/69d47061d9307d440127d32751429a62 to your computer and use it in GitHub Desktop.
inversion of control pattern for object-oriented javascript
// the simplest possible example of inversion of control
class IOC
{
/*
* @type {string}
*/
classProperty: 'hello world'
constructor(/** Function */ fn) {
// provide class context
fn.call(this)
return this
}
}
// the first parameter is a function with access
// to the class context - logic can be directed
// by the instantiating expression, rather than
// by the class internals.
new IOC(
function() {
console.log( this.classProperty )
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment