Front-End/JavaScript

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ๊ณผ ์‹คํ–‰๋ฐฉ๋ฒ•

๐ŸŒณ์ง€ํ˜œ์˜ ์ˆฒ 2022. 12. 9. 17:45

๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Counter</title>
</head>

<body>
    <div id="counter">0</div>
    <button id="increase">+</button>
    <button id="decrease">-</button>
    <script>
        // ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์ฝ”๋“œ: ์„ ํƒ์ง€๋Š” 'couter-x'๊ฐ€ ์•„๋‹ˆ๋ผ 'counter'๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
        const counter = document.getElementById('counter-x');
        const increase = document.getElementById('increase');
        const decrease = document.getElementById('decrease');

        let num = 0;
        const render = function () {
            counter.innerHTML = num;
        }

        increase.onclick = function () {
            num++;
            console.log('increase ๋ฒ„ํŠผ ํด๋ฆญ', num);
            render();
        };

        decrease.onclick = function () {
            num--;
            console.log('decrease ๋ฒ„ํŠผ ํด๋ฆญ', num);
            render();
        };
    </script>
</body>

</html>
๋””๋ฒ„๊น…๋ฐฉ๋ฒ•
1. ์ฝ˜์†”์ฐฝ์„ ์—ด์–ด ์—๋Ÿฌ์˜ ๋งํฌ๋ฅผ ํด๋ฆญํ•œ๋‹ค.
2. ์ž๋™์œผ๋กœ ๋„˜์–ด๊ฐ„ ์†Œ์ŠคํŒจ๋„์— ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ ํ‘œ์‹œ๋œ ์ค„(์™ผ์ชฝ ๋ฒˆํ˜ธ)์„ ํด๋ฆญํ•œ๋‹ค.(๋ธŒ๋ ˆ์ดํฌ ํฌ์ธํŠธ)
3. ์—๋Ÿฌ๋ฅผ ์žฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์›นํŽ˜์ด์ง€์˜ ๋ฒ„ํŠผ(์—๋Ÿฌ๋ฐœ์ƒ ์ง€์ )์„ ๋‹ค์‹œ ํด๋ฆญํ•œ๋‹ค.
4. ๋””๋ฒ„๊น…๋ชจ๋“œ ํ™œ์„ฑํ™”(๋””๋ฒ„๊น… ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋˜์ง€ ์•Š์œผ๋ฉด ์›นํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•œ๋‹ค.)
5. ์†Œ์ŠคํŒจ๋„์˜ ๋ธŒ๋ ˆ์ดํฌ ํฌ์ธํŠธ๋ฅผ ์ฐ์–ด ๋†“์€ ์ค„์— ๋งˆ์šฐ์Šค๋ฅผ ๊ฐ€์ ธ๋‹ค ๋Œ€๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ์›์ธ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

๋‚ด์žฅ ํ„ฐ๋ฏธ๋„

const arr = [1, 2, 3];
arr.forEach(console.log);

// ๋‚ด์žฅ ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  'node [ํŒŒ์ผ์ด๋ฆ„]'์„ ์ž…๋ ฅํ•˜๋ฉด js๋ฅผ ์‹คํ–‰์‹œํ‚จ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.



const arr2 = [1, 2, 3];
arr.forEach(alert);

// "ReferenceError: alert is not defined"
// alertํ•จ์ˆ˜๋Š” ๋ธŒ๋ผ์šฐ์ €์—๋งŒ ๋™์ž‘ํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ Web API(Node.jsํ™˜๊ฒฝ์—์„œ๋Š” ์‹คํ–‰ํ•  ์ˆ˜ ์—†์Œ)
VS Code Live Server ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•œ ์˜ˆ์‹œ๋ฌธ
ํ•ด๋‹น ํŒŒ์ผ์„ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญํ•˜์—ฌ "Open with Live Server"๋ฅผ ์„ ํƒ
๋งค๋ฒˆ ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•˜์ง€ ์•Š์•„๋„ ๊ฐ€์ƒ ์„œ๋ฒ„๊ฐ€ ๊ธฐ๋™๋˜์–ด ๋ธŒ๋ผ์šฐ์ €์— HTML ํŒŒ์ผ์ด ์ž๋™ ๋กœ๋”ฉ๋œ๋‹ค.
๋งค๋ฒˆ ์ƒˆ๋กœ๊ณ ์นจ ํ•˜์ง€ ์•Š์•„๋„ ๋จ.

 

 

 

 

 

์ฐธ๊ณ ์ž๋ฃŒ: http://www.yes24.com/Product/Goods/92742567
 

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive - YES24

ใ€Ž๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Diveใ€์—์„œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‘˜๋Ÿฌ์‹ผ ๊ธฐ๋ณธ ๊ฐœ๋…์„ ์ •ํ™•ํ•˜๊ณ  ๊ตฌ์ฒด์ ์œผ๋กœ ์„ค๋ช…ํ•˜๊ณ , ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ์˜ ๋™์ž‘ ์›๋ฆฌ๋ฅผ ์ง‘์š”ํ•˜๊ฒŒ ํŒŒํ—ค์นœ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ๋ถ„์ด ์ž‘์„ฑํ•œ ์ฝ”๋“œ

www.yes24.com