2016年6月20日 星期一

javascript closure

w3schools對於closure的註解與例子:

註解:

A closure is a function having access to the parent function scope, even after the parent function has closed.


例子:

var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();

add();
add();
add();

// the counter is now 3

closure可以產生不會重置的私有變數

沒有留言:

張貼留言