This is a brief explanation for closure in js.
Closure is a function that returns a function.
Basic Usage:12345678910var saveFn = function (val) { return function () { return val; };};var retriveFn = saveFn(100);console.log(retrieveFn());//100
An argument 100 has been passed to closure, saveFn
.
The function retrieveFn
can now return values returned by saveFn
, but it cannot change the entered value(100).
We can use this as the following:
|
|
same base function adder
, used differently! :)