Day 8 : Learning JS fundamentals, Part -3

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…


This content originally appeared on DEV Community and was authored by Gaurav-Shekhawat

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

Alt Tefxt

In the above example, the variable question will remian alive, even after 100ms of execution of parent function.

Example - 2

Alt Tdext

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.

Alt Texft

Alt dText

DOUBT

Prototypes in js


This content originally appeared on DEV Community and was authored by Gaurav-Shekhawat


Print Share Comment Cite Upload Translate Updates
APA

Gaurav-Shekhawat | Sciencx (2021-08-21T10:42:32+00:00) Day 8 : Learning JS fundamentals, Part -3. Retrieved from https://www.scien.cx/2021/08/21/day-8-learning-js-fundamentals-part-3/

MLA
" » Day 8 : Learning JS fundamentals, Part -3." Gaurav-Shekhawat | Sciencx - Saturday August 21, 2021, https://www.scien.cx/2021/08/21/day-8-learning-js-fundamentals-part-3/
HARVARD
Gaurav-Shekhawat | Sciencx Saturday August 21, 2021 » Day 8 : Learning JS fundamentals, Part -3., viewed ,<https://www.scien.cx/2021/08/21/day-8-learning-js-fundamentals-part-3/>
VANCOUVER
Gaurav-Shekhawat | Sciencx - » Day 8 : Learning JS fundamentals, Part -3. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/21/day-8-learning-js-fundamentals-part-3/
CHICAGO
" » Day 8 : Learning JS fundamentals, Part -3." Gaurav-Shekhawat | Sciencx - Accessed . https://www.scien.cx/2021/08/21/day-8-learning-js-fundamentals-part-3/
IEEE
" » Day 8 : Learning JS fundamentals, Part -3." Gaurav-Shekhawat | Sciencx [Online]. Available: https://www.scien.cx/2021/08/21/day-8-learning-js-fundamentals-part-3/. [Accessed: ]
rf:citation
» Day 8 : Learning JS fundamentals, Part -3 | Gaurav-Shekhawat | Sciencx | https://www.scien.cx/2021/08/21/day-8-learning-js-fundamentals-part-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.