Bank() Function:

2026-10-21来源 dev.to CSSEnglish

function bank() {

let balance = 500; // Private variable function deposit(amt) { balance = balance + amt; // Accessing outer scope variable return balance; } return deposit; 

}
const depositFn = bank();

What happens here?

The bank() function holds an internal variable balance.

​The inner function deposit() updates and returns the balance.

​Even after bank() finishes executing, depositFn remembers the balance variable!

原文:https://www.a8q9.cn/en/article/bank-function-69a6ee.html

相关文章