Day 8 : Learning JS fundamentals, Part -3
Gaurav-Shekhawat
Posted on August 21, 2021
Closures
Closure is when a function "remembers" the variables outside of it, even if you pass that function elsewhere.
function makeAdder(x) {
return function(y) {
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
The full reference can be found on - MDN
In the above example, the variable question will remian alive, even after 100ms of execution of parent function.
Example - 2
Here, the function holdYourQuestion
will remember the question, even if it is called at an entire different time on an entire differnet place.
this
keyword
It is all about the call, it is not the definition of the function, it is not where the function is, none of that matters, it is only how the function was called that determines where the this
keyword will be pointing to.
A this-aware function can thus have a different context each time it's called, which makes it more flexible & reusable.
DOUBT
Prototypes in js
Posted on August 21, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.