16 January 2020
This was almost magic for me.
In javascript functions can have non local inputs in them which are only declared post the function declaration.
function hello() {
console.log(dude);
}
dude = 'Rambo';
hello(); // => Rambo
Elaborating this a bit
let person = (function() {
function status() {
if (person.checked) {
console.log('checked');
} else {
console.log('not checked');
}
}
function checkPerson() {
person.checked = true;
}
return {
checked: false,
check: checkPerson,
status: status,
};
})();
person.status(); // "not checked"
person.check();
person.status(); // "checked"
It boggled me initially how the definition of function checkPerson
was able to
understand what person
is, turns out it’s some js magic!