DEV Community

Discussion on: Understanding Dependency Injection in JavaScript

Collapse
 
silviumihnea profile image
Silviu-Mihnea Cucuiet

What you describe is not dependency injection, but dependency inversion. You made it so the class Pet does not automatically create a dependency, but rather takes it as a parameter. I think an important bit that you left out is the fact that the instance of the Dog class(fluffy) can be reused in other classes. Good point to specify a purpose for dependency inversion is to create more instances of the dependant class with different dependencies. The bit about the injector is right, dependency injection is a pattern where you don't even need to create any of your services / components / dependencies, but rather let a framework create them for you and figure out the order of the creation and injection of the dependencies. I wish you detailed more on that part.

Collapse
 
iliashterevgit profile image
ilia-shterev-git

Without being an expert I think that dependency injection is one form of practical application of the dependency inversion.

Collapse
 
_mohanmurali profile image
Mohan Murali

My aim was to make people understand the concept of loose coupling through dependency injection. This is a beginner focused post and don't want to confuse people with information they might not need. Thanks for reading.

Collapse
 
jeel profile image
JP

right, agree