javascript JS Sorax 15 - Замыкания

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 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());

以上是关于javascript JS Sorax 15 - Замыкания的主要内容,如果未能解决你的问题,请参考以下文章

javascript JS Sorax 13 - Функции

javascript JS Sorax - Обработкаисключений

JavaScript基础15——js的DOM对象

Python自动化开发学习15-JavaScript和DOM

JAVAscript学习笔记 js条件语句 第三节 (原创) 参考js使用表 (2017-09-14 15:55)

学习JavaScript数据结构与算法 2/15