Для получения полного доступа
зарегистрируйтесь.
RSS

All snippets tagged with "scope" tag



samdark
  • Reputation: 313
  • Snippets: 57
  • Revisions: 5

Thanks to closures we can avoid using global flag which spoils the scope.

The code could be executed in any browser console. To trigger it, click anywhere on the page.

window.addEventListener('click', function () {
    var i = 0;
    return function () {
        window.alert('You\'ve clicked it ' + (++i) + ' time(s).');
    };
}());