Learn JavaScript CLOSURES in 10 minutes! 🔒

  Рет қаралды 11,351

Bro Code

Bro Code

Күн бұрын

// 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
00:00:00 intro
00:00:31 example 1
00:02:19 example 2
00:07:00 example 3
00:10:08 conclusion
// --------- 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`);

Пікірлер: 30
@BroCodez
@BroCodez 6 ай бұрын
// 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 6 ай бұрын
you can also write like this. (example 2). return { increment, getCount: () => count, };
@OFAMidoriya
@OFAMidoriya 15 күн бұрын
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 20 күн бұрын
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()_
@MrRe-sj2iv
@MrRe-sj2iv Ай бұрын
Outstanding explanation I've ever seen on KZbin about Closure. Thank you so much bro. Keep up your good work.
@haidermansoor4760
@haidermansoor4760 4 ай бұрын
this is the best channel ever. Thanks alot BRO
@vallunacoder.wecodetogether
@vallunacoder.wecodetogether 4 ай бұрын
the best js tutorial I have ever seen!
@raazyaa
@raazyaa Ай бұрын
What an explanation and what a VOICE ! thx bro !!!
@collinsestell5969
@collinsestell5969 22 күн бұрын
Just...beautifully explained!
@user-cm8ds7rx6t
@user-cm8ds7rx6t Ай бұрын
the best teacher, thanks bro!
@doronfeldman5152
@doronfeldman5152 4 ай бұрын
It the best programming channel!
@rhaissabgl
@rhaissabgl 23 күн бұрын
the best explanation ever!!!!
@k00lmoh
@k00lmoh 6 ай бұрын
that brings back memories ngl, of when i was first learning Classes and i just couldn't get them until i understood closures
@Ankitsarwal
@Ankitsarwal 22 күн бұрын
I like the explaining way
@gauravshrmai1988
@gauravshrmai1988 6 ай бұрын
superb bro !!
@harshjaiswal1634
@harshjaiswal1634 6 ай бұрын
I'm following this playlist
@hongquannguyen5532
@hongquannguyen5532 2 ай бұрын
if there are 3 or 4 functions stack on eachother, would the deep inner function get access to all the outers ?
@juliannafotheringham7101
@juliannafotheringham7101 3 ай бұрын
so amazing thank you
@uzma_azam
@uzma_azam 3 ай бұрын
Thanks Bro 😊
@Martin958
@Martin958 2 ай бұрын
@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 22 күн бұрын
It's a feature of JavaScript ES6 called Shorthand Properties.
@AllHailLordMegatron
@AllHailLordMegatron 18 күн бұрын
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
@nemesisxl6736
@nemesisxl6736 6 ай бұрын
React tutorials pls!!
@nebularzz
@nebularzz 6 ай бұрын
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()
@HikaruAkitsuki
@HikaruAkitsuki Ай бұрын
At this point, I have no idea what's the purpose of class if function have this closure.
@Cinemas19
@Cinemas19 6 ай бұрын
what happens when we create lots of closures in our code?
@Cinemas19
@Cinemas19 6 ай бұрын
It slow-down our application......
@chiggywiggy524
@chiggywiggy524 22 күн бұрын
You answered your own question?
Learn JavaScript setTimeout() in 6 minutes! ⏰
6:13
Bro Code
Рет қаралды 6 М.
JavaScript Closures Tutorial (Explained in depth)
19:03
ColorCode
Рет қаралды 61 М.
Dynamic #gadgets for math genius! #maths
00:29
FLIP FLOP Hacks
Рет қаралды 18 МЛН
g-squad assembles (skibidi toilet 74)
00:46
DaFuq!?Boom!
Рет қаралды 11 МЛН
Eccentric clown jack #short #angel #clown
00:33
Super Beauty team
Рет қаралды 20 МЛН
JavaScript GETTERS & SETTERS are awesome!!! 📐
13:14
Bro Code
Рет қаралды 10 М.
Designing scalable Compose APIs
19:53
Android Developers
Рет қаралды 10 М.
15 crazy new JS framework features you don’t know yet
6:11
Fireship
Рет қаралды 398 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 145 М.
A Worlds First On This Top Tier Radio - TIDRadio H3
11:52
Tech Minds
Рет қаралды 16 М.
Javascript Closure tutorial ( Closures Explained )
12:52
techsith
Рет қаралды 542 М.
Every React Concept Explained in 12 Minutes
11:53
Code Bootcamp
Рет қаралды 360 М.
Differences between Vue and React?
8:09
Vue Mastery
Рет қаралды 20 М.
lvalues and rvalues in C++
14:13
The Cherno
Рет қаралды 300 М.
Dynamic #gadgets for math genius! #maths
00:29
FLIP FLOP Hacks
Рет қаралды 18 МЛН