Mark As Completed Discussion

The self invoking function assigned to minus only executes once, and creates a parent scope that returns a function expression. This function expression counter -= 1; return counter has access to the counter from the parent scope, and abstracts it away from the global scope.

Let's move onto this next bit.

Step 1: Understand Dependency Injection

Dependency Injection (DI) is a design pattern where an object's dependencies are provided from outside rather than being created within the object. It increases modularity and allows for easier testing and reconfiguration. In this challenge, we will implement DI using closures to inject a logging function into different factories.

Step Three

Hence the term-- such dependencies are injected into the object via the constructor or via defined method or a setter property.

This pattern decreases coupling between an object and its dependency, and allows reconfiguration without changing existing business logic. On the flip side, you're locked into certain dependency types and the dependency resolving logic might be abstracted away.

It is another way to using use dependencies from the global context.

We can apply dependency injection using closures. Let's say we had a certain logger we wanted to use, that we needed as a dependency to many factories.

Step 2: Analyzing the Boilerplate Code

The given boilerplate code lays out the structure of the problem:

JAVASCRIPT
1function createFactories(logger) {
2  // TODO: Return an object containing emailFactory and smsFactory
3  // emailFactory should take a greeting and return a function that takes a greet and logs it using the logger
4  // smsFactory should take a text and return a function that logs the text using the logger
5}

Here, we need to create a function createFactories that accepts a logger function and returns an object containing two factories, emailFactory and smsFactory.

Step 3: Creating the emailFactory

The emailFactory takes a greeting and returns a function that takes a greet. This inner function will use the logger to log the concatenated greeting and greet.

JAVASCRIPT
1emailFactory: function(greeting) {
2  return function(greet) {
3    logger(greeting + greet);
4  };
5}

Step 4: Creating the smsFactory

Similarly, the smsFactory will take some text and return a function that logs the text using the logger.

JAVASCRIPT
1smsFactory: function(text) {
2  return function(text) { logger(text); };
3}

Step 5: Combining emailFactory and smsFactory

We need to combine both factories inside the createFactories function and return them as an object:

JAVASCRIPT
1function createFactories(logger) {
2  return {
3    emailFactory: function(greeting) {
4       return function(greet) {
5          logger(greeting + greet);
6       };
7    },
8    smsFactory: function(text) {
9       return function(text){ logger(text); };
10    }
11  };
12}

Step 6: Understanding the Solution

  • The createFactories function accepts a logger function as a dependency.
  • It returns an object containing two factories, emailFactory and smsFactory, both utilizing the logger.
  • The returned factories are closures that have access to the logger, allowing them to log messages in different ways.
  • By injecting the logger dependency, the code remains flexible and decoupled, allowing different loggers to be used without modifying the factory functions.

This exercise demonstrates how closures can be used to implement Dependency Injection in JavaScript. By building the solution step by step, we created a flexible and modular design where dependencies are injected from outside, enhancing maintainability and testability. The approach aligns with modern best practices in software design, showcasing the power and versatility of closures in managing dependencies.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment