This content originally appeared on Bits and Pieces - Medium and was authored by Aman
A closure is a function that accesses variables “outside” itself, in another function’s scope, even after the parent function has closed. A closure is able to do this because of a saved reference to the lexical environment it was initially defined in. Thus, a closure gives our functions persistent memories.
Let’s take a look at an example.
In the above example, we are saving the result of the outer function into our variable myFunc. When we do this, not only is the internal function of the incrementCounter saved, but also the variable counter. This bundle of the function along with the variable is what is called a closure. This is because of a saved reference to the lexical environment.
So if the combination above is collectively a closure, what exactly is counter and what should we refer to it as? It is basically data stored that is carried with the internal function to be stored in global memory. Some call this “Persistent Lexically Scoped Reference Data”.
We can also say that the counter variable is “closed over” by the internal function of incrementCounter.
Are closures memory efficient? Yes. By using closures, we can create variables that are stored in memory to be used in the future.
Below are some more examples that may appear in interviews
What will be the result of the following?
1 and 1. Every time we invoke the outer function, we reserve some space in local memory that will contain all the variables and arguments. When we finish running the outer function, that temporary memory will be deleted. So when we invoke the outer function again, there is no previously stored memory to access.
What will be the result of the following?
1 and 2. A closure will be created when the function is returned from another function, as on line 12. And because of this closure, we will have persistent lexically scoped reference data that we can access.
What will be the result of the following?
1, 2 and 1, 2. Multiple closure instances don’t share the same persistent memory.
Build composable applications
Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favorite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience.
Bring your team to Bit Cloud to host and collaborate on components together, and greatly speed up, scale, and standardize development as a team. Start with composable frontends like a Design System or Micro Frontends, or explore the composable backend. Give it a try →
Learn More
- How We Build Micro Frontends
- How we Build a Component Design System
- The Composable Enterprise: A Guide
- 7 Tools for Faster Frontend Development in 2022
JavaScript Interview Question: What is a Closure? was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Aman
Aman | Sciencx (2022-03-23T08:57:45+00:00) JavaScript Interview Question: What is a Closure?. Retrieved from https://www.scien.cx/2022/03/23/javascript-interview-question-what-is-a-closure/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.