The Christmas Eve post is about closures. Closures are an interesting property of JavaScript. Basically, it means that you can reference variables in an outer scope from an inner scope. The trivial example is something like this:
const sayIt = (text, when) => {
let task = () => console.log(text);
setTimeout(task, when);
}
This inner anonymous function has access to the text
variable. Basically, this allows a variable to mean exactly the same inside a function as outside.
The more interesting examples of closures are to implement private functions. That looks something like:
const bankAccount = (initialBalance) => {
const balance = initialBalance;
return {
getBalance: function() {
return balance;
},
deposit: function(amount) {
balance += amount;
return balance;
},
};
};
const account = bankAccount(100);
account.getBalance(); // 100
account.deposit(10); // 110
That’s a useful construct.