No video

Closures - Beau teaches JavaScript

  Рет қаралды 74,904

freeCodeCamp.org

freeCodeCamp.org

Күн бұрын

Closures are an important concept in JavaScript and other programming languages. Learn the basics of closures in this video!
Code:
🔗 codepen.io/beau...
More info:
🔗 developer.mozi...
🔗 medium.com/jav...
Beau Carnes on Twitter: / carnesbeau
⭐JavaScript Playlists⭐
▶ES6: • ES6 - Beau teaches Jav...
▶JavaScript Basics: • JavaScript Basics Course
▶Design Patterns: • Design Patterns - Beau...
▶Data Structures and Algorithms: • Data Structures and Al...
▶Clean Code: • Clean Code - Beau teac...
-
We're busy people who learn to code, then practice by building projects for nonprofits. Learn Full-stack JavaScript, build a portfolio, and get great references with our open source community.
Join our community at freecodecamp.com
Follow us on twitter: / freecodecamp
Like us on Facebook: / freecodecamp
Follow Quincy on Quora: www.quora.com/...

Пікірлер: 70
@emilewamsteker3412
@emilewamsteker3412 5 жыл бұрын
So far, this is one of the best descriptions of closures I've come across. However, when you explain closures, it's worth doing a brief review of function syntax. I think what confuses most people about closures is the return statement of the outer function, what exactly is being returned. Most people believe the return statement is returning what is evaluated in the inner function, when, in fact, it is returning the function itself along with its lexical scope. Note, that in the return statement, the displayName function does not have a set of parentheses following it. Remember that a function without parentheses is the function itself -- all of its syntax, instructions, lexical scope-- and NOT the value of the evaluated function, as most people might think. In this example, when the inner function is passed to the global scope, it gets stored to a variable in much the same manner as a function expression. As with a function expression, to invoke it, you place parentheses after it to call its respective function, and it evaluates. I've have yet to see anyone explain this in a video. Their is an assumption that the viewer already understands this concept. The problem with learning closures is that by the time a student gets to them, they are so used to the syntax of function declarations, that they take them for granted and probably do not realize that only the function name is used in the return statement, and not a function call.
@olutobiogunsola7610
@olutobiogunsola7610 5 жыл бұрын
I will get this concept someday.
@coliwong6018
@coliwong6018 4 жыл бұрын
Emile, thinking that you maybe explain more clearly and some people may not know how function is called in stack. Actually after finishing of calling a function, all of which are destroyed.But a new copy of that survives in the surrounding environment. That is why we can use it later.
@nwokporochukwuebuka
@nwokporochukwuebuka 2 жыл бұрын
Well said. I agree with all you have said
@kifkifa56
@kifkifa56 5 жыл бұрын
After writing JavaScript code for the last couple of years now I understand what exactly is javascript closure. You made my day bro. Thanks alot.
@dostaquitos1215
@dostaquitos1215 5 жыл бұрын
I am still confused but less confused than after my 30min lecture + activities on this. Thank you for being an excellent teacher!
@Av-fn5wx
@Av-fn5wx 11 ай бұрын
Thanks Beau,. After 2 years as a JS engineer, finally understood closures watching a 6 years old video
@RainOnline
@RainOnline 5 жыл бұрын
Well you always manage to explain it the way I can understand it. So to everyone else, we all learn different! for me Beau is the way..
@jbrabec6811
@jbrabec6811 4 жыл бұрын
Awesome example! This helped me a ton when you related it to an object-oriented class with private data and public and protected functions
@keep1hunnid
@keep1hunnid 2 жыл бұрын
Wow, just spend at least the past 2 hours trying to understand closures watching a few 20+ minute videos, reading up, etc. This video is literally the shortest content out of any of them but it is the only one that is clear and logical and it definitely helped me understand closures. Thank you!
@zs302
@zs302 7 жыл бұрын
Thanks for the great video and tutorial. This is how I like to think of closures. Please feel free to correct me if my understanding of some concepts aren't correct as I'm just learning about closures myself. /* The closure of a function is the function(s) its enclosed in and the inner function(s) has access to the variables defined in those closures even after those functions are no longer on the execution stack. Paste and run the below in Google Chrome console and drill down into the scopes to check the closures out. In the below example the inner function is enclosed in speak function hence it's closure is the function speak. If speak itself was within another outer function then inner function has TWO closures the speak function as well as the outer one enclosing speak. */ function speak(greeting) { var inner = function (name) { console.log(greeting + " " + name); } return inner } var theGreeting = speak("Hello"); console.log(theGreeting("Adam")); console.dir(theGreeting);
@aj_schwifty8458
@aj_schwifty8458 4 жыл бұрын
Z S I don’t know why but your extremely short paragraph was the best explanation I’ve ever gotten of closures lol. It’s given ME closure *badum tss*
@Woodshadow
@Woodshadow 7 жыл бұрын
A month in on this stuff and I am still completely lost
@BeauCarnes
@BeauCarnes 7 жыл бұрын
Have you tried going through the JavaScript training on freecodecamp.com? Also, I'm releasing the videos sort of out of order but by the end of this month I will have a complete basic javascript course that you can then watch in order.
@Eli7PM
@Eli7PM 5 жыл бұрын
You should try to understand what a Scope is first and then tackle Closures. This is my recommendation to you. :)
@GoatzAreEpic
@GoatzAreEpic 5 жыл бұрын
2 years now how u doin lmao
@hongfrank3398
@hongfrank3398 5 жыл бұрын
u r not alone.
@dmitrik5566
@dmitrik5566 2 жыл бұрын
so, the private variable in the parent container function is modified by the child container function? Ex. function parentFunction() { let counter = 0 function childFunction() { console.log("counter",counter) console.log("counter increment",counter+=1) } return childFunction } let result = parentFunction() result() result() result()
@min11benja
@min11benja 3 жыл бұрын
It's when a function or variable has access to other variables within the scope or the environment it was created in. They are sometimes used to emulate private methods. Which can only be called by other methods in the same class. Since JS does not have a native way of doing this.
@min11benja
@min11benja 3 жыл бұрын
I got this somewhere else : What is closure? It is where an inner function has access to the outer enclosing functions variables. A closure has access to basically 3 scope chains, its own scope variables, the outer functions variables, and the global variables.
@bulldawg4498
@bulldawg4498 4 жыл бұрын
Yes, one of the better explanations I've come across, too ... Concise, too ...
@swarmdesignsolutions8098
@swarmdesignsolutions8098 4 жыл бұрын
this is by far the best video - thanks
@kokomi5858
@kokomi5858 5 жыл бұрын
You get it simple, and you still get it well done.
@henry3939
@henry3939 5 жыл бұрын
Super helpful and straight to the point! Thanks a lot for sharing.
@biljam972
@biljam972 4 жыл бұрын
Thank you! I was really confused by closures.
@scottsmyth3154
@scottsmyth3154 2 жыл бұрын
Great explanation thanks!
@centrumsaiyan7623
@centrumsaiyan7623 4 жыл бұрын
Thanks, I have watched several great tutorial channels and yet, closure is still something i'm not confident talking about.
@sorcerir
@sorcerir 5 жыл бұрын
I logged in to say thank you. Gosh I finally understand what closures are. Wait, do I really???? lol
@osamagamal495
@osamagamal495 5 жыл бұрын
a man once said you won't get closure even if you think you've completely got it!
@sourandbitter3062
@sourandbitter3062 5 жыл бұрын
So just to make sure, in the second example there is no garbage collection on the environment the returned object is because that returned object references everything that is in it? How does this example compare to an ES6 class, or for the private part, classes on Typescript?
@Flowstyletattoo
@Flowstyletattoo 5 жыл бұрын
Just had a break through. Thank you.
@Grahfx
@Grahfx 4 жыл бұрын
It looks like a class emulation no ? You just instantiate an object called counter that has a property privateCounter and 3 methods in it.
@MistaJones89
@MistaJones89 4 жыл бұрын
Thanks for this, this helped give me a little more clarity with one of the lessons I'm learning in JavaScript on udemy. Liked and subscribed.
@robtangled8816
@robtangled8816 2 жыл бұрын
The only problem I notice with Beau's teaching method is that he goes from 20km/h to 150km/h in one second. If you try to play it at 0.75x, sometimes it will feel too slow, but if you play it at 1x, he speaks too fast 30% of the time.
@tantill134
@tantill134 7 жыл бұрын
Thanks for the example!
@PulpFreePress
@PulpFreePress 5 жыл бұрын
Clearly explained. Thanks!
@bobsutton4320
@bobsutton4320 7 жыл бұрын
Thanks for the example. It was an eye-opener. However, this appeared to reveal a bug in Firefox's JavaScript engine. The open curly bracket MUST be on the same line as the "return" statement. If one follows my bracketing convention, where the bracket is placed on the following line, the engine generates an error. However, if the object being returned is placed in a variable first, which is then returned, it works regardless of where the opening bracket is located.
@alessandroyorba8187
@alessandroyorba8187 7 жыл бұрын
I'm not sure if that's a bug. When the the file is read at runtime, javascript engines will automatically insert semi-colons to the end of each line. So I believe it placed that semicolon right after your return keyword, ultimately ending the function and returning "Undefined"
@raregamer7162
@raregamer7162 5 жыл бұрын
Hi I am a little confused on how the privateCounter variable isn't getting destroyed and starting from 0 again every time counter is called, and is able to actually keep track of the count, as if it was a global variable. Is it that the variable counter is actually storing that information and doesn't destroy the values?
@curiouspolyglot
@curiouspolyglot 7 жыл бұрын
Thank you, that was an awesome explanation!!
@osamagamal495
@osamagamal495 5 жыл бұрын
Good tutorial about closure. thanks
@kyliestaraway2492
@kyliestaraway2492 4 жыл бұрын
Maybe you should tell people that whatever is in the closures will be stored there even many new lines away.
@hashtagtrends9886
@hashtagtrends9886 4 жыл бұрын
Thank you!
@susmoys
@susmoys Жыл бұрын
what is the use case of this?
@mortyjr2334
@mortyjr2334 5 жыл бұрын
Private methods in Java: private void printSomething() { System.out.println("I'm better"); } Private methods in JavaScript: This video
@Ghosidd
@Ghosidd 5 жыл бұрын
Great explanation of IIFS + Closures. /neededthat
@DanT-iu6oc
@DanT-iu6oc 4 жыл бұрын
still confused as all hell. you're glossing over subjects and not going over it thoroughly. GO MORE SLOWLY AND STEP BY STEP BY STEP BY STEP BY STEP. Amazes me how much CS skips steps when by nature it is all about logical steps
@himynameisoleg
@himynameisoleg 4 жыл бұрын
I finally understand!
@regular5571
@regular5571 6 жыл бұрын
This is some trick stuff. Thanks for the explanation though, its a bit more clear to me.
@ashwin81088
@ashwin81088 4 жыл бұрын
You are awesome!
@abdullahgumel3936
@abdullahgumel3936 3 жыл бұрын
So basically it's like a character in a sandbox game having access to all the stuff in their environment they exist within
@betterbeavailable
@betterbeavailable 5 жыл бұрын
that example is ripped straight from developer.mozilla.org/en-US/docs/Web/JavaScript/Closures :)
@renanalves5687
@renanalves5687 4 жыл бұрын
3:04 Access
@stanislavcoros
@stanislavcoros 4 жыл бұрын
this is complete mess explanation. ... That second attempt.
@anasu8040
@anasu8040 6 жыл бұрын
closures ? hard
@neahnderthal
@neahnderthal 3 жыл бұрын
would greatly appreciate if you talk louder instead of adjusting my speaker every time :(
@sebastian-nunez
@sebastian-nunez 5 жыл бұрын
coming from Java this behavior is quite odd, but ok, i guess xD
@ianpaul0520
@ianpaul0520 3 жыл бұрын
Uh. What!?
@SANJEETkumar-yl5kt
@SANJEETkumar-yl5kt 5 жыл бұрын
This is what I can NEVER understand and I never know why? I feel like I should better die.
@freecodecamp
@freecodecamp 5 жыл бұрын
Just keep trying and you will eventually get it! Try starting at the beginning of the free curriculum at www.freecodecamp.org.
@ultimatelokal7040
@ultimatelokal7040 3 жыл бұрын
ugh my brain hurts.
@ajcics
@ajcics 4 жыл бұрын
Dude...you cant use jargon with beginners. This was terrible
@ebiekem
@ebiekem 5 жыл бұрын
Mehn, JavaScript is hard...
@skverskk
@skverskk 4 жыл бұрын
Way too confusing of a presentation
@pannihto7588
@pannihto7588 4 жыл бұрын
I do understand closures (or at least I think I do) but that's a horrible explanation
THIS keyword - Beau teaches JavaScript
8:48
freeCodeCamp.org
Рет қаралды 58 М.
5 Essential JavaScript Interview Questions
20:32
Coding With Chaim
Рет қаралды 91 М.
Unveiling my winning secret to defeating Maxim!😎| Free Fire Official
00:14
Garena Free Fire Global
Рет қаралды 11 МЛН
나랑 아빠가 아이스크림 먹을 때
00:15
진영민yeongmin
Рет қаралды 6 МЛН
Zombie Boy Saved My Life 💚
00:29
Alan Chikin Chow
Рет қаралды 29 МЛН
JavaScript Closures Tutorial (Explained in depth)
19:03
ColorCode
Рет қаралды 68 М.
Closures in JS 🔥 | Namaste JavaScript Episode 10
22:44
Akshay Saini
Рет қаралды 813 М.
Javascript interview | Closure
11:44
Hitesh Choudhary
Рет қаралды 36 М.
Learn Closures In 13 Minutes
13:22
Web Dev Simplified
Рет қаралды 65 М.
Lexical scoping and Closure | chai aur #javascript
26:48
Chai aur Code
Рет қаралды 85 М.
20 String Methods in 7 Minutes - Beau teaches JavaScript
7:00
freeCodeCamp.org
Рет қаралды 132 М.
JavaScript Closures Explained Simply
8:43
DevSage
Рет қаралды 6 М.
The JavaScript Survival Guide
14:47
Fireship
Рет қаралды 725 М.
Javascript Closure Tutorial | Closures Explained
17:58
Dave Gray
Рет қаралды 145 М.
Copying Arrays (deep and shallow) - Beau teaches JavaScript
5:56
freeCodeCamp.org
Рет қаралды 45 М.
Unveiling my winning secret to defeating Maxim!😎| Free Fire Official
00:14
Garena Free Fire Global
Рет қаралды 11 МЛН