Learn JavaScript CLOSURES in 10 minutes! 🔒

  Рет қаралды 22,564

Bro Code

Bro Code

Күн бұрын

Пікірлер: 40
@BroCodez
@BroCodez Жыл бұрын
// closure = A function defined inside of another function, // the inner function has access to the variables // and scope of the outer function. // Allow for private variables and state maintenance // Used frequently in JS frameworks: React, Vue, Angular // ---------- EXAMPLE 1 ---------- function outer(){ const message = "Hello"; function inner(){ console.log(message); } inner(); } message = "Goodbye"; outer(); // ---------- EXAMPLE 2 ---------- function createCounter() { let count = 0; function increment() { count++; console.log(`Count increased to ${count}`); } function getCount() { return count; } return {increment, getCount}; } const counter = createCounter(); counter.increment(); counter.increment(); counter.increment(); console.log(`Current count: ${counter.getCount()}`); // ---------- EXAMPLE 3 ---------- function createGame(){ let score = 0; function increaseScore(points){ score += points; console.log(`+${points}pts`); } function decreaseScore(points){ score -= points; console.log(`-${points}pts`); } function getScore(){ return score; } return {increaseScore, decreaseScore, getScore}; } const game = createGame(); game.increaseScore(5); game.increaseScore(6); game.decreaseScore(3); console.log(`The final score is ${game.getScore()}pts`);
@volkan9799
@volkan9799 11 ай бұрын
you can also write like this. (example 2). return { increment, getCount: () => count, };
@OFAMidoriya
@OFAMidoriya 5 ай бұрын
if you ever redo this video can you explain ex 2 more I understood everything until 4:00 and the same with ex3 every thing was ok until 9:30
@benzflynn
@benzflynn 6 ай бұрын
This is the only video on closures that I see here which *clearly explains the purpose* of closures. Nice tip on shorthand form of return statement. It's common to use a functional expression for the outer holding function of the closure, i.e. _const game = function() {_ _. . . . ._ _. . . . ._ _. function increaseScore (points){_ _. .}_ _. function decreaseScore (points){_ _. .}_ _. function getScore (){_ _. .}_ _._ _}_ Then on the calling code, you just reference each of the inner functions with the prefix of the outer one, i.e. > _game.getScore()_
@orangelimesky
@orangelimesky 4 ай бұрын
This was perfectly explained. Now I understood closures. It's just a fancy name and the way it's written everywhere else is confusing because it makes it look like it can only return one function. But it's basically an object that references multiple functions within, to protect a variable from being public. So simple. Thank you.
@newmagicfilms
@newmagicfilms 2 ай бұрын
Seeing this similar to a Class , and private members, was easier to grasp
@haidermansoor4760
@haidermansoor4760 9 ай бұрын
this is the best channel ever. Thanks alot BRO
@prashant6804
@prashant6804 4 ай бұрын
You explained in the context of class & objects so helpful for person like me who breath in & out object oriented programming since I started programming in 2004 with Java. Thanks!
@steventolerhan5110
@steventolerhan5110 4 ай бұрын
how does this video only have 500 likes, legitimately the most succinct explanation I've seen so far!
@skalskaga
@skalskaga 3 ай бұрын
this is the best closures-related yt video!
@vallunacoder.wecodetogether
@vallunacoder.wecodetogether 9 ай бұрын
the best js tutorial I have ever seen!
@doronfeldman5152
@doronfeldman5152 9 ай бұрын
It the best programming channel!
@Ankitsarwal
@Ankitsarwal 6 ай бұрын
I like the explaining way
@rhaissabgl
@rhaissabgl 6 ай бұрын
the best explanation ever!!!!
@Zoooooooooooo
@Zoooooooooooo 8 күн бұрын
hawk tuah
@tonnytrumpet734
@tonnytrumpet734 3 ай бұрын
Thanks, very well explained. Just hurts to watch a little if you are learning this after working with any OOP language for some time
@raazyaa
@raazyaa 6 ай бұрын
What an explanation and what a VOICE ! thx bro !!!
@ФёдорСёмочкин
@ФёдорСёмочкин 6 ай бұрын
the best teacher, thanks bro!
@MrRe-sj2iv
@MrRe-sj2iv 7 ай бұрын
Outstanding explanation I've ever seen on KZbin about Closure. Thank you so much bro. Keep up your good work.
@harshjaiswal1634
@harshjaiswal1634 11 ай бұрын
I'm following this playlist
@collinsestell5969
@collinsestell5969 6 ай бұрын
Just...beautifully explained!
@k00lmoh
@k00lmoh 11 ай бұрын
that brings back memories ngl, of when i was first learning Classes and i just couldn't get them until i understood closures
@hongquannguyen5532
@hongquannguyen5532 7 ай бұрын
if there are 3 or 4 functions stack on eachother, would the deep inner function get access to all the outers ?
@Martin958
@Martin958 7 ай бұрын
@4:17 What did you mean by return the object {increment: increment} ? Why would the function be both the property and the value?
@DavidFerreira-ld5gp
@DavidFerreira-ld5gp 6 ай бұрын
It's a feature of JavaScript ES6 called Shorthand Properties.
@bennymeister
@bennymeister 3 ай бұрын
If you pass an already existing variable name into an object, it will create an entry in the object with the variable value, like this: const foo = 69 const obj = { foo: foo } // { foo: 69 } const obj2 = { foo } // { foo: 69 } You see how these are ultimately just two ways of writing the same thing. One being shorter than the other
@AllHailLordMegatron
@AllHailLordMegatron 5 ай бұрын
it seems closure works like a class but JS has classes , so why do we use closure ? does anyone know the difference ?
@FahimAhamed-oc8nh
@FahimAhamed-oc8nh 5 ай бұрын
We can use closure for small state managements of the elements ,whereas classes are used for more complex stuff
@juliannafotheringham7101
@juliannafotheringham7101 8 ай бұрын
so amazing thank you
@gauravshrmai1988
@gauravshrmai1988 Жыл бұрын
superb bro !!
@nemesisxl6736
@nemesisxl6736 11 ай бұрын
React tutorials pls!!
@uzma_azam
@uzma_azam 8 ай бұрын
Thanks Bro 😊
@nebularzz
@nebularzz 11 ай бұрын
wouldn't this also be a closure function some_other_function(closure) { closure() } function some_function() { let message = "Hello, world!" let closure = function() { console.log(message) } some_other_function(closure) } some_function()
@kcseanbrixd.calinao6700
@kcseanbrixd.calinao6700 2 сағат бұрын
definitely just a primitive version of class. What a great pov
@Cinemas19
@Cinemas19 11 ай бұрын
what happens when we create lots of closures in our code?
@Cinemas19
@Cinemas19 11 ай бұрын
It slow-down our application......
@chiggywiggy524
@chiggywiggy524 6 ай бұрын
You answered your own question?
@HikaruAkitsuki
@HikaruAkitsuki 7 ай бұрын
At this point, I have no idea what's the purpose of class if function have this closure.
@SquidysTents
@SquidysTents 4 ай бұрын
I’m guessing it’s probably to abstract away a lot of the confusion and complexity of closures like this, but I don’t really know either haha
Learn JavaScript setTimeout() in 6 minutes! ⏰
6:13
Bro Code
Рет қаралды 11 М.
JavaScript Closures Tutorial (Explained in depth)
19:03
ColorCode
Рет қаралды 73 М.
PIZZA or CHICKEN // Left or Right Challenge
00:18
Hungry FAM
Рет қаралды 9 МЛН
Disrespect or Respect 💔❤️
00:27
Thiago Productions
Рет қаралды 27 МЛН
I built the same app 10 times // Which JS Framework is best?
21:58
Fireship
Рет қаралды 2,6 МЛН
Learn JavaScript ARROW FUNCTIONS in 8 minutes! 🎯
8:02
Bro Code
Рет қаралды 39 М.
Learn Closures In 13 Minutes
13:22
Web Dev Simplified
Рет қаралды 69 М.
JavaScript Visualized - Execution Contexts
11:41
Lydia Hallie
Рет қаралды 67 М.
How to FETCH data from an API using JavaScript ↩️
14:17
Bro Code
Рет қаралды 149 М.
Build this JS calculator in 15 minutes! 🖩
15:20
Bro Code
Рет қаралды 641 М.
JavaScript FUNCTIONS are easy! 📞
12:14
Bro Code
Рет қаралды 43 М.
One CPU To Rule Them All - Ryzen 7 9800X3D Review
12:47
Linus Tech Tips
Рет қаралды 1 МЛН
JavaScript GETTERS & SETTERS are awesome!!! 📐
13:14
Bro Code
Рет қаралды 18 М.
JavaScript Interview Prep: Functions, Closures, Currying
1:29:03
freeCodeCamp.org
Рет қаралды 156 М.