Bird
Raised Fist0

What is wrong with this dependency injection usage?

medium๐Ÿ“ Analysis Q7 of Q15
LLD - Advanced LLD Concepts
What is wrong with this dependency injection usage?
class Service {
  constructor(repo) {
    this.repo = repo;
  }
}
const service = new Service();
AService class cannot have dependencies
BConstructor should not have parameters
CMissing dependency argument when creating Service instance
DNo error, code is valid
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor parameters and instantiation

    Service expects a 'repo' argument but none is provided when creating instance.
  2. Step 2: Identify error cause

    Missing argument will cause runtime error or undefined behavior.
  3. Final Answer:

    Missing dependency argument when creating Service instance -> Option C
  4. Quick Check:

    Constructor missing argument = Error [OK]
Quick Trick: Always provide dependencies when instantiating classes manually [OK]
Common Mistakes:
MISTAKES
  • Ignoring constructor parameters
  • Assuming default arguments exist
  • Thinking classes cannot have dependencies

Want More Practice?

15+ quiz questions ยท All difficulty levels ยท Free

Free Signup - Practice All Questions
More LLD Quizzes