Skip to content

Instantly share code, notes, and snippets.

@RinatValiullov
Last active June 30, 2017 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RinatValiullov/10539ff0aeee0e1fa9728e12894d2b86 to your computer and use it in GitHub Desktop.
Save RinatValiullov/10539ff0aeee0e1fa9728e12894d2b86 to your computer and use it in GitHub Desktop.
Learn Closure (замыкание)
// Enclosing function (Внешняя (родительская) функция )
function numberGenerator() {
	var num; // локальная "свободная" переменная

	// Внутренняя (дочерняя) функция. Имеет доступ к переменной "num" благодаря замыканию
	function checkNumber(num) {
		num++; // Увеличиваем на единицу входящий параметр
		console.log(num); // Выводим в консоль
	}
	return checkNumber; // Возвращаем дочернюю функцию
}

var number = numberGenerator(); // создаем экземпляр внешней функции
var numb = 5;
number(numb); // Вызываем и получаем "numb = numb + 1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment