studies/Back-end
[Javascript] ๋ฒํผ ๋๋ฅด๋ฉด ๊ฐ ์ฆ๊ฐ(๋์ )ํ๋ ๋ก์ง
Vada Kim
2020. 3. 31. 13:16
728x90
๋ฐ์ํ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Closures</h2>
<p>Counting with a local variable.</p>
<button type="button" onclick="myFunction()">Count!</button>
<p id="demo">0</p>
<script>
var add = (function () {
var counter = 0;
return function () {counter += 1; return counter;}
})();
function myFunction(){
document.getElementById("demo").innerHTML = add();
}
</script>
</body>
</html>
โ
์ฌ์ค ์ด ๋ฐฉ๋ฒ์ด ์ดํด๊ฐ ์์ง ์ ๊ฐ์ง ์๋๋ค. add() ํจ์๊ฐ ์คํ๋ ๋๋ง๋ค ํจ์ ์์ ์ง์ญ๋ณ์๊ฐ 0์ผ๋ก ์ด๊ธฐํ๋๋๋ฐ ์ด๋ป๊ฒ ๊ธฐ์กด ๊ฐ์ ๊ฐ์ ธ์ค๋๊ฑด์ง...
์๊ณ ๋ฆฌ์ฆ์ ๊ณต๋ถํด์ผ ํ ๊ฒ ๊ฐ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฆฌ๋ทฐ์๋ค.
728x90
๋ฐ์ํ