Sign in
Log inSign up
David  Morais

209 likes

·

6.5K reads

11 comments

Manthankumar Satani
Manthankumar Satani
Apr 14, 2021

Great compilations with specific comments. I learned few things, thanks.

1
·
·1 reply
David  Morais
David  Morais
Author
·Apr 14, 2021

Thanks 👋

1
·
sascha sascha
sascha sascha
Apr 14, 2021

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.

1
·
·4 replies
David  Morais
David  Morais
Author
·Apr 14, 2021

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.

1
·
Tony B
Tony B
Apr 14, 2021

David Morais

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.

·
sascha sascha
sascha sascha
Apr 14, 2021

David Morais

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.

·
David  Morais
David  Morais
Author
·Apr 14, 2021

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.

2
·
Karol
Karol
Apr 14, 2021

Hi! On the screenshot under the nullish operator you've got it wrong.

null ?? "foo" =/= null
·
·2 replies
David  Morais
David  Morais
Author
·Apr 14, 2021

Hey, thanks for reading 👋 You are absolutely right, I need to change that.

Thanks for catching that.

EDIT: Updated 🚀

2
·
Karol
Karol
Apr 14, 2021

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

1
·
Golden Azubuike
Golden Azubuike
Apr 15, 2021

Educative and cool

·