Created
January 2, 2020 21:47
-
-
Save peterjanes/537be540b3568dc3fc7c568b537d5568 to your computer and use it in GitHub Desktop.
JavaScript class-based dependency injection
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
// implementing http://misko.hevery.com/2010/05/29/dependency-injection-and-javascript-closures/ with classes | |
class Factory { | |
constructor(alert) { | |
return { | |
greetFactory(greeting) { | |
return new GreetFactory(alert, greeting) | |
} | |
} | |
} | |
} | |
class GreetFactory { | |
constructor(alert, greeting) { | |
return function(greet) { | |
alert(greeting + greet) | |
} | |
} | |
} | |
const factory = new Factory(console.log) | |
const greet = factory.greetFactory('Hello ') | |
greet('Misko') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment