Last active
April 15, 2020 01:44
-
-
Save matthewstokeley/69d47061d9307d440127d32751429a62 to your computer and use it in GitHub Desktop.
inversion of control pattern for object-oriented javascript
This file contains 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
// 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