Kommilitonen im Kurs JS Closures an der LMU München. erstellen und teilen Zusammenfassungen, Karteikarten, Lernpläne und andere Lernmaterialien mit der intelligenten StudySmarter Lernapp. Jetzt mitmachen!
Jetzt mitmachen!
JS Closures
It is function + lexical environment
In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is called, at function creation time.
Closure is when a function can remember and access its lexical scope even when it's invoked outside its lexical scope. (e.g. when it is passed around as a value to a variable).
The inner function will have access to the variables in the outer function scope, even after the outer function has returned.
var add = (function () {
var counter = 0;
return function () {counter += 1; return counter}
})();
add();
add();
add();
// the counter is now 3
JS Closures
JS Closures
JS Closures
Basically, when Javascript compiles all of your code, all variable declarations using var are hoisted/lifted to the top of their functional/local scope (if declared inside a function) or to the top of their global scope (if declared outside of a function) regardless of where the actual declaration has been made. This is what we mean by “hoisting”.
Function declarations are also hoisted, but these go to the very top, so will sit above all of the variable declarations.
However, a key thing to note is that the only thing that gets moved to the top is the variable declarations , not the actual value given to the variable. (so it returns undefined)
what we have to remember though is that the concept of hoisting is not a literal process (ie, the declarations themselves do not move to the top of the file — it is simply a process of the JavaScript compiler reading them first in order to create space in memory for them).
All identifiers are registered to their respective scopes during compile time. Moreover, every identifier is created at the beginning of the scope it belongs to, every time that scope is entered. The term for registering a variable at the top of its enclosing scope, even though its declaration may appear further down in the scope, is called hoisting.
let and const will throw reference errors. This is because although let and const are hoisted, they are not initialised until the code executes the initialising line. The time between a let or const being hoisted and initialised is commonly know as the temporal dead zone.
JS Closures
JS Closures
JS Closures
JS Closures
JS Closures
JS Closures
JS Closures
JS Closures
Für deinen Studiengang JS Closures an der LMU München gibt es bereits viele Kurse auf StudySmarter, denen du beitreten kannst. Karteikarten, Zusammenfassungen und vieles mehr warten auf dich.
Zurück zur LMU München ÜbersichtsseiteStudySmarter ist eine intelligente Lernapp für Studenten. Mit StudySmarter kannst du dir effizient und spielerisch Karteikarten, Zusammenfassungen, Mind-Maps, Lernpläne und mehr erstellen. Erstelle deine eigenen Karteikarten z.B. für JS Closures an der LMU München oder greife auf tausende Lernmaterialien deiner Kommilitonen zu. Egal, ob an deiner Uni oder an anderen Universitäten. Hunderttausende Studierende bereiten sich mit StudySmarter effizient auf ihre Klausuren vor. Erhältlich auf Web, Android & iOS. Komplett kostenfrei. Keine Haken.
Bestes EdTech Startup in Deutschland
Du bist schon registriert? Hier geht‘s zum Login