LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

JS
// Closure is a reference to a functions lexial scope outside of that functions execution this does *NOT* necessarily mean static values i.e. if that executed function contains a reference and that reference is updated you will still get the updated references values i.e:

let showMeTheClosure;
let a = 1;

(function setup(){
  let b = a;
  let c = 1;
  showMeTheClosure = function(){
    return b + c;
  };
})();

// setup() has been executed and is no longer available

console.log(showMeTheClosure()); // 2

// Just referenced setup() inner scope to access `b = a` & c = 1`

(function closureWithUpdatedRefernce(){
  a = 5;
  console.log(showMeTheClosure()); // 3
})();

// Updated `a` which setup() `b` refences so output of showMeTheClosure() changes
Term
Wed, 12/27/2017 - 07:01

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv