Skip to main content

Closure

@serverSerrverlesskiy

A closure is a function⚙️ that has access to an external function⚙️ even after the external function️ has terminated. A closure is needed to provide an internal function with access to the scope of an external function️, but at the same time close access from the external environment to the variables of the internal function⚙️.

Snake

Requirements for creating a closure:

  1. An external function that is called in the code.
  2. The outer function contains the inner function.
  3. As a result, the outer function returns the inner one.

Let's consider creating a closure using an example:

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^
  1. In the example, we have created an external function getFruit;
  2. Inside getFruit created an internal function show.
  3. The getFruit function returns the show function as a result.
  4. Further in the code, we assigned the result of the getFruit function to the variable showFruit.
  5. Because the result of getFruit is a function, then showFruit becomes not a variable🔔, but a function.
  6. The result of the whole construction is the variable fruit, which is inside the function getFruit, it became closed. Now we can only find out the value of this variable🔔, we cannot change it.

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


IIo3iTiv

📖

Dmitriy Vasilev

💵

Resoner2005

🐛 🎨 🖋

Navernoss

🖋 🐛 🎨

Become a Patron!