DEV Community

Discussion on: Dependency Injection in JavaScript 101

Collapse
 
roassys profile image
Rodrigo Assis

Incredible! But one doubt: when you'll use ".register", you've to specify first the label and then the class, right?

Collapse
 
jeremylikness profile image
Jeremy Likness ⚡️

Correct. In the simple DI example you pass a label (string), then an array that should have the function constructor, factory, or instance as the last element.

Collapse
 
roassys profile image
Rodrigo Assis

Great! And in this case: "$jsInject.register("engine", ["pistons", Engine]);", you have to pass the "pistons" in the array too because "Engine" receives "pistons" as a parameter, right? And then the last element would be "Engine" cuz is the "current class".

Thread Thread
 
jeremylikness profile image
Jeremy Likness ⚡️

You got it! The important part of the illustration is that "pistons" is just a label, not an implementation, so you have flexibility to define it however you see fit elsewhere.

Thread Thread
 
roassys profile image
Rodrigo Assis

Oh, I get it! Then I'll use this label on "$jsInject.get()". Amazing!! Thanks ;)