Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!
“发现功能JavaScript”被BookAuthority评为最佳新功能编程书籍之一 !
A closure is an inner function that has access to the outer scope, even after the outer scope has executed.
闭包是一个内部函数,即使在执行外部范围之后,也可以访问外部范围。
范围 (Scope)
Scope defines the lifetime and visibility of a variable.
范围定义变量的生存期和可见性。
Every time we create a function or a block {}
, we create a new scope.
每次创建函数或块{}
,我们都会创建一个新范围。
Variables declared with var
have only function scope. Variables declared with let
and const
can have function and block scope.
用var
声明的变量仅具有函数作用域。 用let
和const
声明的变量可以具有函数和块范围。
A closure is a inner function. It has access to variables from the outer scopes.
闭包是内部函数。 它可以访问外部作用域中的变量。
Variables used by the closure function live as long as the closure lives.
闭包函数使用的变量与闭包生存的时间一样长。
Closures make it easy to work with async tasks like timers, events, AJAX calls.
使用闭包可以轻松处理异步任务,例如计时器,事件,AJAX调用。
Closures create encapsulation.
封闭创建封装。
Read Functional Architecture with React and Redux and learn how to build apps in function style.
阅读具有React和Redux的功能架构,并学习如何以函数样式构建应用程序。
Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!
发现功能JavaScript被称为 BookAuthority最好的新功能编程书籍 !
For more on applying functional programming techniques in React take a look at Functional React.
有关在React中应用函数式编程技术的更多信息,请查看 Functional React 。
You can find me on Medium and Twitter.
您可以在Medium和Twitter上找到我。
翻译自: https://www.freecodecamp.org/news/discover-the-power-of-closures-in-javascript-5c472a7765d7/