maksimerohin
6/4/2019 - 12:08 PM

JS Sorax 15 — Замыкания

var func = function() {
  var i = 10;
  return function() {
    return i;
  }
};

var anotherFunc = function() {
  var i = 20;
  console.log(func()());
}

anotherFunc();
var counter = (function() {
  var count = 0;
  return function() {
    return count++;
  }
}());

console.log(counter());
console.log(counter());
console.log(counter());
console.log(counter());
var counter = (function() {
  var count = 0;
  return function(num) {
    count = num !== undefined ? num : count;
    return count++;
  }
}());

console.log(counter());
console.log(counter());
console.log(counter(0));
console.log(counter());
console.log(counter());
console.log(counter());
var counter = function(num) {
  counter.count = num !== undefined ? num : counter.count;
  return counter.count++;
}

counter.count = 0;

console.log(counter());
console.log(counter());
console.log(counter());
console.log(counter());