209 likes
·
6.5K reads
11 comments
Great compilations with specific comments. I learned few things, thanks.
Thanks 👋
Nice article, good visualizations.
But your explanation of closures is not correct and it happens a lot. You are describing lexical scoping as far as I remember.
A closure "closes" the function.
function a() {
let me = 0;
function b(){
me = 1;
}
return b;
}
let fA = a(); // function is still "in the memory"
fA(); // now it is getting removed "from the memory"
The final execution of b() by calling fA() closes a(). So the name isn't even that bad.
Hey 👋 Tks for reading and comenting
My example is minimalist, at best, but is still a closure, at least according to MDN's explanation. I have access to the outer function/scope variables while inside an inner function. I just happen to be in a theoretical global scope
Your example is correct, but I think fA() will remain in the memory heap until it's garbage collected.
I don't think you are correct about your example being a closure - even according to MDN's explanation.
"a function bundled together (enclosed) with references to its surrounding state"
You took the bit about surrounding state, but ignored the bit about a function being bundled together/enclosed. Your example is just looking at global state - that isn't bundled, that is open to everyone to mess with. :)
I would update it anyway for teaching purposes, as the rest of your article is excellent IMHO.
yes it is still there I think therefore the quotes. as Tony B said I would change it because the word closure is about the enclosed state and not purely about scope and your article is very good.
I'll update it later today, but I'm still in doubt wether this implicit global scope doesn't count as an enclosure. Tony B thanks for the read and the feedback 🤘🤘
Thank you both
EDIT: Updated both snippets.
Hi! On the screenshot under the nullish operator you've got it wrong.
null ?? "foo" =/= null
Hey, thanks for reading 👋 You are absolutely right, I need to change that.
Thanks for catching that.
EDIT: Updated 🚀
David Morais Great, thanks! Sorry that my first comment is pointing out mistakes instead of congratulating you on a nice article, which it obviously is ;) Thanks for you work and input :D
Educative and cool