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/