Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
•••
YouTube Security VideosGoogle DeepMind: From deepfakes to DNA: the science of watermarking AI(01.10.2026 um 18:37 Uhr)
•••
YouTube Security VideosAndroid Police: Apple has put itself in an awkward position.(01.10.2026 um 19:30 Uhr)
•
Podcasts & Audio Briefings9to5Google: iPhone 18 Pro vs. Pixel 11 Pro isn't a contest.(01.10.2026 um 17:30 Uhr)
•
YouTube Security VideosCHIP: iPhone 18 Pro (Max) im Test-Fazit: Das beste iPhone, aber …(01.10.2026 um 19:30 Uhr)
•
YouTube Security Videos2026 winners of the Intel Hardware Security Academic Awards | Intel(01.10.2026 um 18:00 Uhr)
••••
YouTube Security VideosGoogle DeepMind: From deepfakes to DNA: the science of watermarking AI(01.10.2026 um 18:37 Uhr)
•••
YouTube Security VideosAndroid Police: Apple has put itself in an awkward position.(01.10.2026 um 19:30 Uhr)
•
Podcasts & Audio Briefings9to5Google: iPhone 18 Pro vs. Pixel 11 Pro isn't a contest.(01.10.2026 um 17:30 Uhr)
•
YouTube Security VideosCHIP: iPhone 18 Pro (Max) im Test-Fazit: Das beste iPhone, aber …(01.10.2026 um 19:30 Uhr)
•
YouTube Security Videos2026 winners of the Intel Hardware Security Academic Awards | Intel(01.10.2026 um 18:00 Uhr)
•
Intelligence View
⚡ tsecurity.de Intelligence

Some basic understanding of closure🎈

About closure Quoting Wikipedia: In computer science, a closure, also known as a lexical closure or function closure, is a technique for implementing lexical…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!




About closure



Quoting Wikipedia: In computer science, a closure, also known as a lexical closure or function closure, is a technique for implementing lexical binding in programming languages ​​that support first-class functions. In implementation, a closure is a structure that stores a function (usually its entry address) and an associated environment (equivalent to a symbol lookup table).



The above description is very professional. I prefer to describe it this way: the ability of a function to remember and access variables in the scope in which it was defined. This characteristic is often used extensively in functional programming languages.



This type of code is typically written in JavaScript:




Why can the count variable still be accessed outside the outer function?

Because the outer function returns an inner function, and the inner function holds a reference to count, the local variable count will not be destroyed after outer finishes executing.





function outer() {
let count = 0;

return function inner() {
count++;
console.log(count);
};
}

const fn = outer();

fn(); // 1
fn(); // 2






This demonstrates an important function of closures: they extend the lifespan of variables. When an inner function references an outer variable, that variable cannot be released and must be moved to the heap to survive.




Closure -> A function carries the context in which it was defined







The most common use of closures






Data privatization



Using closures prevents external code from directly modifying a variable.




function createCounter() {
let count = 0;

return {
inc() {
count++;
},
get() {
return count;
}
};
}

const c = createCounter();

c.inc();
c.inc();

console.log(c.get());









Function Factory






function multiply(x) {
return function(y) {
return x * y;
};
}

const double = multiply(2);

console.log(double(5));









React hooks



A simple implementation of useState:




let hooks = [];
let currentIndex = 0;

function useState(initialValue) {
const index = currentIndex;

hooks[index] = hooks[index] ?? initialValue;

function setState(newValue) {
hooks[index] = newValue;
render();
}

currentIndex++;

return [hooks[index], setState];
}


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Some basic understanding of closure🎈

Thematisch verwandte Begriffe: Some, basic, understanding, closure · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag