jsclosure2

by rootjang

HTML

<!DOCTYPE html>
<html lang="ko-KR">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <p>클로저 counting</p>
  <button id="inclease">+</button>
  <p id="counter">0</p>
  <script>
    var count = document.getElementById('counter');

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

    document.getElementById('inclease').onclick = function () {
      count.innerHTML = increaser();
    };
  </script>
</body>
</html>