Home > Web Front-end > JS Tutorial > body text

The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh)

PHPz
Release: 2024-07-29 04:22:03
Original
443 people have browsed it

The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh)

Wanted to share here, the simplest explanation that I have ever seen on the web, for the famous Closures topic. (Credits : roadmap.sh)

A closure is a function that has access to its outer function scope even after the outer function has returned. This means a closure can remember and access variables and arguments of its outer function even after the function has finished.

function outer() {
  const name = 'Roadmap';

  function inner() {
    console.log(name);
  }

  return inner;
}

const closure = outer();
closure(); // Roadmap

Copy after login

In the above example, the inner function has access to the name variable of the outer function even after the outer function has returned. Therefore, the inner function forms a closure.

The above is the detailed content of The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh). For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!