index.html

Download
html 320 lines 7.5 KB
  1<!DOCTYPE html>
  2<html lang="ko">
  3<head>
  4    <meta charset="UTF-8">
  5    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6    <title>JavaScript ๊ธฐ์ดˆ ์˜ˆ์ œ</title>
  7    <style>
  8        body {
  9            font-family: 'Segoe UI', Tahoma, sans-serif;
 10            max-width: 800px;
 11            margin: 0 auto;
 12            padding: 20px;
 13            background-color: #f5f5f5;
 14        }
 15        h1 { text-align: center; color: #333; }
 16        h2 { color: #2c3e50; border-bottom: 2px solid #f39c12; padding-bottom: 10px; }
 17        .section {
 18            background: white;
 19            padding: 20px;
 20            margin: 20px 0;
 21            border-radius: 8px;
 22            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
 23        }
 24        .output {
 25            background: #2c3e50;
 26            color: #2ecc71;
 27            padding: 15px;
 28            border-radius: 4px;
 29            font-family: monospace;
 30            white-space: pre-wrap;
 31            max-height: 300px;
 32            overflow-y: auto;
 33        }
 34        button {
 35            background: #3498db;
 36            color: white;
 37            border: none;
 38            padding: 10px 20px;
 39            border-radius: 4px;
 40            cursor: pointer;
 41            margin: 5px;
 42        }
 43        button:hover { background: #2980b9; }
 44        code {
 45            background: #ecf0f1;
 46            padding: 2px 6px;
 47            border-radius: 3px;
 48        }
 49        pre {
 50            background: #ecf0f1;
 51            padding: 15px;
 52            border-radius: 4px;
 53            overflow-x: auto;
 54        }
 55    </style>
 56</head>
 57<body>
 58    <h1>JavaScript ๊ธฐ์ดˆ ์˜ˆ์ œ</h1>
 59    <p style="text-align: center;">F12๋ฅผ ๋ˆŒ๋Ÿฌ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์˜ ์ฝ˜์†”์„ ํ™•์ธํ•˜์„ธ์š”.</p>
 60
 61    <!-- ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์˜์—ญ -->
 62    <div class="section">
 63        <h2>์ถœ๋ ฅ ๊ฒฐ๊ณผ</h2>
 64        <div class="output" id="output">๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๊ธฐ์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.</div>
 65        <button onclick="clearOutput()">์ดˆ๊ธฐํ™”</button>
 66    </div>
 67
 68    <!-- 1. ๋ณ€์ˆ˜ -->
 69    <div class="section">
 70        <h2>1. ๋ณ€์ˆ˜ (var, let, const)</h2>
 71        <button onclick="runVariables()">์‹คํ–‰</button>
 72        <pre>
 73// var - ํ•จ์ˆ˜ ์Šค์ฝ”ํ”„ (๋น„๊ถŒ์žฅ)
 74var oldVar = "I'm var";
 75
 76// let - ๋ธ”๋ก ์Šค์ฝ”ํ”„, ์žฌํ• ๋‹น ๊ฐ€๋Šฅ
 77let count = 0;
 78count = 1;
 79
 80// const - ๋ธ”๋ก ์Šค์ฝ”ํ”„, ์žฌํ• ๋‹น ๋ถˆ๊ฐ€
 81const PI = 3.14159;
 82const person = { name: "ํ™๊ธธ๋™" };
 83person.name = "๊น€์ฒ ์ˆ˜"; // ๊ฐ์ฒด ์†์„ฑ์€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
 84        </pre>
 85    </div>
 86
 87    <!-- 2. ๋ฐ์ดํ„ฐ ํƒ€์ž… -->
 88    <div class="section">
 89        <h2>2. ๋ฐ์ดํ„ฐ ํƒ€์ž…</h2>
 90        <button onclick="runDataTypes()">์‹คํ–‰</button>
 91        <pre>
 92// ๊ธฐ๋ณธ ํƒ€์ž… (Primitive)
 93let str = "๋ฌธ์ž์—ด";      // String
 94let num = 42;            // Number
 95let float = 3.14;        // Number (์ •์ˆ˜์™€ ๊ตฌ๋ถ„ ์—†์Œ)
 96let bool = true;         // Boolean
 97let empty = null;        // Null
 98let notDefined;          // Undefined
 99let sym = Symbol("id");  // Symbol
100let big = 9007199254740991n; // BigInt
101
102// ์ฐธ์กฐ ํƒ€์ž… (Reference)
103let obj = { key: "value" };  // Object
104let arr = [1, 2, 3];         // Array
105let func = function() {};    // Function
106        </pre>
107    </div>
108
109    <!-- 3. ์—ฐ์‚ฐ์ž -->
110    <div class="section">
111        <h2>3. ์—ฐ์‚ฐ์ž</h2>
112        <button onclick="runOperators()">์‹คํ–‰</button>
113        <pre>
114// ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž
115let a = 10, b = 3;
116// +, -, *, /, %, **
117
118// ๋น„๊ต ์—ฐ์‚ฐ์ž
119// ==, ===, !=, !==, <, >, <=, >=
120// == vs === (ํƒ€์ž… ๋น„๊ต ์ฐจ์ด)
121
122// ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž
123// && (AND), || (OR), ! (NOT)
124
125// ์‚ผํ•ญ ์—ฐ์‚ฐ์ž
126let result = a > b ? "ํฌ๋‹ค" : "์ž‘๋‹ค";
127
128// Nullish coalescing (??)
129let value = null ?? "๊ธฐ๋ณธ๊ฐ’";
130
131// Optional chaining (?.)
132let obj = { nested: { value: 42 } };
133let safe = obj?.nested?.value;
134        </pre>
135    </div>
136
137    <!-- 4. ์กฐ๊ฑด๋ฌธ -->
138    <div class="section">
139        <h2>4. ์กฐ๊ฑด๋ฌธ</h2>
140        <button onclick="runConditions()">์‹คํ–‰</button>
141        <pre>
142// if-else
143if (์กฐ๊ฑด1) {
144    // ์‹คํ–‰
145} else if (์กฐ๊ฑด2) {
146    // ์‹คํ–‰
147} else {
148    // ์‹คํ–‰
149}
150
151// switch
152switch (๊ฐ’) {
153    case 'A':
154        break;
155    case 'B':
156        break;
157    default:
158        break;
159}
160        </pre>
161    </div>
162
163    <!-- 5. ๋ฐ˜๋ณต๋ฌธ -->
164    <div class="section">
165        <h2>5. ๋ฐ˜๋ณต๋ฌธ</h2>
166        <button onclick="runLoops()">์‹คํ–‰</button>
167        <pre>
168// for
169for (let i = 0; i < 5; i++) { }
170
171// while
172while (์กฐ๊ฑด) { }
173
174// do-while
175do { } while (์กฐ๊ฑด);
176
177// for...of (๋ฐฐ์—ด ์ˆœํšŒ)
178for (const item of array) { }
179
180// for...in (๊ฐ์ฒด ํ‚ค ์ˆœํšŒ)
181for (const key in object) { }
182
183// forEach, map, filter, reduce
184        </pre>
185    </div>
186
187    <!-- 6. ํ•จ์ˆ˜ -->
188    <div class="section">
189        <h2>6. ํ•จ์ˆ˜</h2>
190        <button onclick="runFunctions()">์‹คํ–‰</button>
191        <pre>
192// ํ•จ์ˆ˜ ์„ ์–ธ์‹
193function greet(name) {
194    return `Hello, ${name}!`;
195}
196
197// ํ•จ์ˆ˜ ํ‘œํ˜„์‹
198const greet = function(name) {
199    return `Hello, ${name}!`;
200};
201
202// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
203const greet = (name) => `Hello, ${name}!`;
204
205// ๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜
206const greet = (name = "Guest") => `Hello, ${name}!`;
207
208// Rest ํŒŒ๋ผ๋ฏธํ„ฐ
209const sum = (...numbers) => numbers.reduce((a, b) => a + b, 0);
210
211// ์ฝœ๋ฐฑ ํ•จ์ˆ˜
212setTimeout(() => console.log("1์ดˆ ํ›„"), 1000);
213        </pre>
214    </div>
215
216    <!-- 7. ๋ฐฐ์—ด -->
217    <div class="section">
218        <h2>7. ๋ฐฐ์—ด</h2>
219        <button onclick="runArrays()">์‹คํ–‰</button>
220        <pre>
221const fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์˜ค๋ Œ์ง€"];
222
223// ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ
224fruits.push("ํฌ๋„");     // ๋์— ์ถ”๊ฐ€
225fruits.pop();            // ๋์—์„œ ์ œ๊ฑฐ
226fruits.unshift("๋”ธ๊ธฐ");  // ์•ž์— ์ถ”๊ฐ€
227fruits.shift();          // ์•ž์—์„œ ์ œ๊ฑฐ
228
229// ๊ณ ์ฐจ ํ•จ์ˆ˜
230const numbers = [1, 2, 3, 4, 5];
231numbers.map(n => n * 2);           // [2, 4, 6, 8, 10]
232numbers.filter(n => n > 2);        // [3, 4, 5]
233numbers.find(n => n > 2);          // 3
234numbers.reduce((acc, n) => acc + n, 0); // 15
235numbers.every(n => n > 0);         // true
236numbers.some(n => n > 4);          // true
237        </pre>
238    </div>
239
240    <!-- 8. ๊ฐ์ฒด -->
241    <div class="section">
242        <h2>8. ๊ฐ์ฒด</h2>
243        <button onclick="runObjects()">์‹คํ–‰</button>
244        <pre>
245// ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด
246const person = {
247    name: "ํ™๊ธธ๋™",
248    age: 25,
249    greet() {
250        return `์•ˆ๋…•ํ•˜์„ธ์š”, ${this.name}์ž…๋‹ˆ๋‹ค.`;
251    }
252};
253
254// ์†์„ฑ ์ ‘๊ทผ
255person.name;      // ์  ํ‘œ๊ธฐ๋ฒ•
256person["age"];    // ๋Œ€๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ•
257
258// ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น
259const { name, age } = person;
260
261// ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž
262const newPerson = { ...person, city: "์„œ์šธ" };
263
264// Object ๋ฉ”์„œ๋“œ
265Object.keys(person);    // ["name", "age", "greet"]
266Object.values(person);  // ["ํ™๊ธธ๋™", 25, ...]
267Object.entries(person); // [["name", "ํ™๊ธธ๋™"], ...]
268        </pre>
269    </div>
270
271    <!-- 9. ํด๋ž˜์Šค -->
272    <div class="section">
273        <h2>9. ํด๋ž˜์Šค (ES6)</h2>
274        <button onclick="runClasses()">์‹คํ–‰</button>
275        <pre>
276class Animal {
277    constructor(name) {
278        this.name = name;
279    }
280
281    speak() {
282        return `${this.name} makes a sound.`;
283    }
284}
285
286class Dog extends Animal {
287    constructor(name, breed) {
288        super(name);
289        this.breed = breed;
290    }
291
292    speak() {
293        return `${this.name} barks!`;
294    }
295}
296
297const dog = new Dog("๋ฉ๋ฉ์ด", "์ง„๋—๊ฐœ");
298        </pre>
299    </div>
300
301    <!-- 10. ๋ชจ๋“ˆ -->
302    <div class="section">
303        <h2>10. ๋ชจ๋“ˆ (ES6)</h2>
304        <pre>
305// math.js
306export const PI = 3.14159;
307export function add(a, b) { return a + b; }
308export default class Calculator { }
309
310// main.js
311import Calculator, { PI, add } from './math.js';
312import * as math from './math.js';
313        </pre>
314        <p><em>๋ชจ๋“ˆ์€ ์„œ๋ฒ„ ํ™˜๊ฒฝ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.</em></p>
315    </div>
316
317    <script src="script.js"></script>
318</body>
319</html>