Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Closures and Dependency Injection (Main Thread)

Here is the interview question prompt, presented for reference.

In JavaScript, closures and dependency injection are powerful concepts that enable modular, maintainable, and flexible code. In this challenge, you will explore these concepts by implementing a specific functionality using closures and then using closures for dependency injection.

Part 1: Closures

Closures allow a function to access variables from an outer function that has already finished its execution. Your task is to create a closure that serves as a decrementing counter.

Boilerplate code: js var minus = (function () { // TODO: Initialize a counter variable // TODO: Return a function that decrements the counter and returns the current value })();

Part 2: Dependency Injection with Closures

Dependency Injection is a technique where one object supplies the dependencies of another object. You will create a function that returns factories for sending email and SMS messages, and these factories will use a logger function passed as a dependency.

Boilerplate code: js function createFactories(logger) { // TODO: Return an object containing emailFactory and smsFactory // emailFactory should take a greeting and return a function that takes a greet and logs it using the logger // smsFactory should take a text and return a function that logs the text using the logger }

You can see the full challenge with visuals at this link.

Challenges • Asked almost 7 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Nov 30, 2017:

This is the main discussion thread generated for Closures and Dependency Injection.