๐ฐclosure
JS โฉ value โฉ function โฉ closure
A closure
is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
gives you access to an outer function's scope from an inner function.
is created every time a function is created, at function creation time.
๐ lexical environment
it matters from which scope your closure closes in variables.
๐ scopes matter with closuresโ
้่ชชไผผไนๆไธๅๅฅๆช็ "for-init" scope๏ผไฝ่ฃก้ข let ่ฎๆธ็่กจ็พๅ ถๅฏฆๆดๅๆฏ block-scopedใ๐ scopes matter with closures โฉ โ
closure: manage grades - closure can be used as a function with private properties.
closure as object - closure can be used as an object.
lexical environment - stores all local variables.
[[Environment]] - reference to surrounding lexical environment.
replit๏ผclosures
// โข `clsr` closes over parameter `a`
// โญโโclsrโโโฎ
let sum = a => b => a + b ;
sum(3)(4), // 7
sum(5)(-1), // 4a closure is usually a nested function.
Last updated
Was this helpful?