Closure

In notebook:
FrontEndMasters Functional light
Created at:
2016-09-24
Updated:
2016-09-24
Tags:
Functional Programming JavaScript Fundamentals

Closure is when a function "remembers" the variables around it even when that function is executed elsewhere"

Shows an example of closure when you return a new function from a surrounding function. This is not the only way to create closure. Other examples he mentions: click handler, timeouts, ajax calls, callbacks
  function foo() {
  var count = 0;
  return function () {
    return count++;
  };
}

var x = foo();

x(); // 0
x(); // 1
x(); // 2
This is not a common pattern in functional programming (the shown example). The returned function doesn't return the same value every time it's run. 
Make it more "functional" by currying or partial application
  function sumX(x) {
  return function(y) {
    return x + y;
  };
}

var add10 = sumX(10);

add10(3);   // 13
add10(14);  // 24
In this pattern, we still have closure, but ​x​ never changes. There's no side effect, where we get a different result on each run.