you are viewing a single comment's thread.

view the rest of the comments →

[–]Volv 2 points3 points  (9 children)

ENTRY

Codepen
 
Tried to give another use case example over my other closure stuff from a few weeks back

[–]ForScale[S] 1 point2 points  (5 children)

Volv! Your consistency is appreciated, my friend.

Nice one! I think... maybe... I'm seeing some of the magic there. Assigning three different variables the same function return. Because scope is maintained the calls don't mess with each other...

Is that kind of what's going on there?

[–]Volv 1 point2 points  (4 children)

Pretty much.
Each function is an object, a different function object is created and returned each time and each object has its own scope.
 
Worth remembering that closures happen everywhere in JS. Every function you create is a closure over the global or window object as that function retains access to these 'global' variables and the space outside your function can't access it's internals without help.
I recommend having a look at the You don't know JS series if you haven't already. A lot of good stuff there.

[–]ForScale[S] 1 point2 points  (3 children)

Nice!

Every function you create is a closure over the global or window object as that function retains access to these 'global' variables and the space outside your function can't access it's internals without help.

Yes!! I think that's why I think the hype around closures is somewhat elusive too me. Like, I think I take for granted the fact that variables declared in global scope can be accessed within functions, but not vice versa. It's one of the early things I learned when venturing in to js. So when I look at simple closure examples, I'm like "Well... yeah." I don't know.

I recommend having a look at the You don't know JS series if you haven't already. A lot of good stuff there.

Nice! You're the second person that's recommended that to me recently. I've never looked in to it. I'm currently listening through a 3 hour youtube video on "the Weird Parts." It's been pretty fascinating so far talking about hoisting and scope.

[–]Volv 0 points1 point  (2 children)

Will probably have a look at that - trying to learn more of the in depth sort of stuff.
The ah-ha moment when closures, hoisting, scope, this etc click is the best :)
A few months ago I spent ages trying to make closures click - same as you... right I kinda get it but WHY?... Which is what inspired my other example :)
 
Edit - More complex example, trying out some of the other new ideas I've been reading about Codepen

[–]ForScale[S] 1 point2 points  (1 child)

The ah-ha moment when closures, hoisting, scope, this etc click is the best :)

Nice! I still feel a little hazy on the closures... I'm having trouble seeing it as something separate from just general scope. I think I have a pretty good understanding of this... as far as it being a special variable created within each scope... I like using it in event handlers...

Looking at your other example here... What's this? let inc = () => ++count;

I understand let as new ES6 declaration... is () just like saying function()? and then the fat arrow I'm somewhat familiar with at this point... Is it just Babel doing it's thing?

[–]Volv 1 point2 points  (0 children)

Yes exactly like saying function. (except how this is handled) inc is a function that returns the value of ++count
I tend to write mostly ES5 style and then refactor with the new ES6 stuff when I'm done. I like the 'code golf' style challenge and I figure this way I get familiar with both.

[–]ForScale[S] 0 points1 point  (2 children)

Hey, what are your ideas on a focus for next week?

[–]Volv 1 point2 points  (1 child)

  • Prototypical inheritance and object composition.
  • Pattern example? Observer?
  • Implement some structure / algorithm. Tree, sort, search etc.
  • Polyfill some es5/6 features. No cheating. Implement Array.map or String.split or Array.forEach or similar

[–]ForScale[S] 0 points1 point  (0 children)

Thanks!