This video covers JavaScript closures. What is a closure? Why is it useful? The context explored is a closure function that animates a DOM element in the browser.
All examples: https://github.com/shiffman/Video-Lesson-Materials
Contact: https://twitter.com/shiffman
All JavaScript/p5.js videos: https://www.youtube.com/user/shiffman/playlists?shelf_id=14&view=50&sort=dd
Help us caption & translate this video!
http://amara.org/v/Qbta/
Original source
39 responses to “9.6: JavaScript Closure – p5.js Tutorial”
Awesome explanation!
Why not define timeIt function outside makeTimer and pass the counter?!! easier to read. This is like anonymous methods in C# or Java.
THANK YOU!!! This totally flicked on a few lightbulbs!
I wanted to say it's a great video, but I think I still didn't get everything about closure.
please i need contact you
Why using closure instead of class and instance ? You could create 2 instances of a class "makeTimer" and store in each : a counter, a method setInterval() and timeIt()
Your videos are AMAZING 😉
So how do you get rid of the closures? Would you have to use clearInterval somehow?
so think of closure as enclouser duh…. thank you
Has he done more examples?
It's not difficult:
Closure is when a function (child), inside another function (parent), has access to its parent variables.
The inverse does not apply, a parent function cannot see variables of its children functions.
dude that just nested functions ,it doesn't really show the meaning of closures ,i mean closures are indeed meant to be done with some returned functions that will operate later when we pass them another ,parameter.
ain't that ?
It's amazing how you explain things my mind just goes click and I totally understand it. Absolute gift of teaching.
Would you please consider doing a full Vanilla JavaScript tutorial??
I can't express the joy I have watching your videos and you've explained this in such a fantastic way that resonates with me so well, keep up the excellent work.
Please share the source code
Why do these californian IT types always wear dorky t-shirts and an open top shirt ??
You are the best man! You have a particular way to explain, thank you for the class I really get it!
Increase speed to 1.5X … thank me later
This was a great example. Thanks!
thank you
Next on the list to help your hatred of JS, context coercion! 😛
come on man, foo() and bar() always suffice for examples 🙂 Or is that usually just a PHP inside joke type thing?
https://www.youtube.com/watch?v=CQqwU2Ixu-U also very good!
Hello Mr Train, you are very silly but you really know your stuff. This is the best video Ive seen on this topic. Its seem that closures are really hard to do but very difficult to explain. Thank you.
Really well-explained.
great video
lund ho tum
Thank you for simplifying things
LOOK what I made using this !
https://codepen.io/rk2208/pen/rpBpYx
cool, very useful
Very interesting! Thank you. What is about setInterval if it starts after click for example? Clicking again and again make new intimation more and more. How we should protect code of new clicking animation all over again?
Why wouldn’t you put timeIt outside of the makeTimer scope and pass in the elt and counter references though?
Great video!
Awesomest videos on youtube, I draw inspiration from you as a teacher, your method of teaching is similar to mine though you're literally the Epicest person on youtube
Epic Dude!!! I'm so sorry about one thing though……………………….
.
.
.
.
.
.
.
.
.
I found your videos too late !!!!
Finally!!! I found a video example of CLOSURE that makes clear sense. Not only does it make clear sense but it's fun to watch. Goofy, silly, insightful, genius, captivating, pure educational entertainment. Highly recommended.
Dan, I love you, I owe you 🙂 These videos are very helpful and colorful.
I have a question:
I am trying to synchronize the start time of both timers, so I tried the code below in the setup() function:
makeTimer(timer1, 1000);
setTimeout(makeTimer(timer2, 500), 1000);
the rest of the code is same with the one here. Interestingly program is working but setTimeout() does't seem to work. timer2 still starts 500 milliseconds before timer1.
Anyone?
I love that you don't edit your mistake :p but it makes me scream.
The best explanation I've seen on a closure, thank you!
I owe you my career. Thank you so much for your dedication to teaching!