
๐ ํจ์
- ์ฐธ๊ณ ์๋ฃ: ํจ์ - JavaScript | MDN
1. ํจ์ ์ ์ธ ๋ฐฉ์
function ํค์๋ ์ฌ์ฉ
function square(number) {
return number * number;
}
- ๋งค๊ฐ๋ณ์ ๊ฐ ๋ณ๊ฒฝ์ ์ ์ญ์ ์ผ๋ก ์ ์ฉ๋์ง ์์.
- ๋งค๊ฐ๋ณ์๊ฐ ๋ฐฐ์ด์ธ ๊ฒฝ์ฐ์๋ ์ ์ญ์ ์ผ๋ก ์ ์ฉ
2. ํจ์ ํํ์
๋ณ์๋ฅผ ํตํด ํจ์๋ฅผ ํ ๋น
const square = function (number) {
return number * number;
};
const x = square(4); // `x` ์ ๊ฐ์ 16 ์
๋๋ค.
ํจ์ ํํ์์์๋ ํจ์ ์ด๋ฆ์ ์ง์ ํ ์ ์๋ค.
const factorial = function fac(n) {
return n < 2 ? 1 : n * fac(n - 1);
};
console.log(factorial(3));
3. ํ์ดํ ํจ์
(ES6์์ ์ถ๊ฐ)
๊ฐ๊ฒฐํ ํจ์ ๋ฌธ๋ฒ์ผ๋ก, function ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , => (ํ์ดํ)๋ฅผ ์ฌ์ฉํด ํจ์๋ฅผ ์ ์ธํ๋ค.
const ํจ์๋ช
= (๋งค๊ฐ๋ณ์) => {
// ํจ์ ๋ณธ๋ฌธ
};
์์:
const add = (a, b) => {
return a + b;
};
console.log(add(2, 3)); // ์ถ๋ ฅ: 5
๋งค๊ฐ๋ณ์๊ฐ ํ๋์ผ ๋ ๊ดํธ๋ฅผ ์๋ตํ ์ ์๋ค.
const square = x => x * x;
console.log(square(4)); // ์ถ๋ ฅ: 16
๋ฐํ๊ฐ์ด ํ ์ค์ผ ๋ ์ค๊ดํธ์ return ํค์๋๋ฅผ ์๋ตํ ์ ์๋ค.
const multiply = (a, b) => a * b;
console.log(multiply(2, 3)); // ์ถ๋ ฅ: 6
ํ์ดํ ํจ์๋ this๊ฐ ์ ์ ์ผ๋ก ๋ฐ์ธ๋ฉ๋๋ค. ์ฆ, ํ์ดํ ํจ์๋ ์์ ์ ๊ฐ์ธ๊ณ ์๋ ์ธ๋ถ ์ค์ฝํ์ this ๊ฐ์ ๊ทธ๋๋ก ์ฌ์ฉํ๋ค. ์ผ๋ฐ ํจ์์์๋ ํธ์ถ ์์ ์ ๋ฐ๋ผ this๊ฐ ๋ฌ๋ผ์ง์ง๋ง, ํ์ดํ ํจ์๋ ์ ์ธ๋ ๋์ this๋ฅผ ๊ณ ์ ํด์ ์ฌ์ฉํ๋ค.
// this ๋์ ์ฐจ์ด
const person = {
name: "Alice",
sayName: function() {
console.log(this.name); // 'this'๋ person ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด
}
};
person.sayName(); // ์ถ๋ ฅ: "Alice"
// ํ์ดํ ํจ์ ์ฌ์ฉ ์:
const person2 = {
name: "Bob",
sayName: () => {
console.log(this.name); // 'this'๋ ์ธ๋ถ ์ค์ฝํ๋ฅผ ๊ฐ๋ฆฌํด
}
};
person2.sayName(); // ์ถ๋ ฅ: undefined (๊ธ๋ก๋ฒ ๊ฐ์ฒด์ 'this' ์ฌ์ฉ)
ํ์ดํ ํจ์๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ์์ ํนํ ์ ์ฉํ๋ฉฐ, ์ฝ๋ฐฑ ํจ์๋ก ์ฌ์ฉํ ๋ this๋ฅผ ๋ฐ๋ก ๋ฐ์ธ๋ฉํ์ง ์์๋ ๋๋ค.
์์:
// ๋ฐฐ์ด ๋ฉ์๋์ ํจ๊ป ์ฌ์ฉ
const numbers = [1, 2, 3, 4, 5];
const squared = numbers.map(num => num * num);
console.log(squared); // ์ถ๋ ฅ: [1, 4, 9, 16, 25]
// ๊ฐ๋จํ ์ฝ๋ฐฑ ํจ์
setTimeout(() => {
console.log("1์ด ํ์ ์คํ");
}, 1000);
๐ฉ ๋ฐฐ์ด(Array)
์ฌ๋ฌ ๊ฐ์ ๊ฐ์ ํ๋์ ๋ณ์์ ์ ์ฅํ ์ ์๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ
1. ๋ฐฐ์ด ์ ์ธ
1.1 ๋๊ดํธ ์ฌ์ฉ(๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ๋ฐฉ๋ฒ)
let arr = [1, 2, 3, 4, 5];
1.2 Array ๊ฐ์ฒด ์ฌ์ฉ
let arr = new Array(1, 2, 3, 4, 5);
2. ๋ฐฐ์ด์ ํน์ง
์ธ๋ฑ์ค๋ฅผ ํตํด ์ ๊ทผํ๋ค.
let arr = ["Apple", "Banana", "Orange"];
console.log(arr[0]); // ์ถ๋ ฅ: "Apple"
console.log(arr[1]); // ์ถ๋ ฅ: "Banana"
๋ฐฐ์ด์ ํฌ๊ธฐ๋ ๋์ ์ด๋ค.
arr[3] = "Grape"; // ์๋ก์ด ์์ ์ถ๊ฐ
console.log(arr); // ์ถ๋ ฅ: ["Apple", "Banana", "Orange", "Grape"]
๋ค์ํ ๋ฐ์ดํฐ ํ์ ์ ์ ์ฅํ ์ ์๋ค.
let mixedArray = [1, "Hello", true, null];
3. ๋ฐฐ์ด์ ์ฃผ์ ๋ฉ์๋
3.1 ๋ฐฐ์ด ์ถ๊ฐ/์ญ์
push: ๋ฐฐ์ด์ ๋์ ์์ ์ถ๊ฐ.
let arr = [1, 2, 3];
arr.push(4);
console.log(arr); // ์ถ๋ ฅ: [1, 2, 3, 4]
pop: ๋ฐฐ์ด์ ๋์์ ์์ ์ ๊ฑฐ.
arr.pop();
console.log(arr); // ์ถ๋ ฅ: [1, 2, 3]
shift: ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ์ ๊ฑฐ
arr.shift();
console.log(arr); // ์ถ๋ ฅ: [2, 3]
unshift: ๋ฐฐ์ด์ ์์ ์๋ก์ด ์์๋ฅผ ์ถ๊ฐ
arr.unshift(0);
console.log(arr); // ์ถ๋ ฅ: [0, 2, 3]
3.2 ๋ฐฐ์ด ์ํ
forEach: ๋ฐฐ์ด ๊ฐ ์์์ ๋ํด ํจ์ ์คํ
let arr = [1, 2, 3];
arr.forEach(element => console.log(element));
3.3 ๋ฐฐ์ด ๋ณํ
map: ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ์๋ก์ด ๋ฐฐ์ด๋ก ๋ฐํ.
let arr = [1, 2, 3];
let squaredArr = arr.map(x => x * x);
console.log(squaredArr); // ์ถ๋ ฅ: [1, 4, 9]
filter: ์กฐ๊ฑด์ ๋ง๋ ์์๋ค๋ก๋ง ์ ๋ฐฐ์ด ์์ฑ.
let arr = [1, 2, 3, 4, 5];
let evenArr = arr.filter(x => x % 2 === 0);
console.log(evenArr); // ์ถ๋ ฅ: [2, 4]
reduce: ๋ฐฐ์ด์ ์์๋ค์ ํ๋์ ๊ฐ์ผ๋ก ์ถ์ฝ.
let arr = [1, 2, 3, 4];
let sum = arr.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // ์ถ๋ ฅ: 10
3.4 ๋ฐฐ์ด ๊ฒ์
indexOf(): ํน์ ๊ฐ์ด ๋ฐฐ์ด์ ๋ช ๋ฒ์งธ ์ธ๋ฑ์ค์ ์๋์ง ๋ฐํ (์์ผ๋ฉด -1 ๋ฐํ)
let arr = ["Apple", "Banana", "Orange"];
console.log(arr.indexOf("Banana")); // ์ถ๋ ฅ: 1
find(): ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฐํ
let arr = [1, 2, 3, 4, 5];
let firstEven = arr.find(x => x % 2 === 0);
console.log(firstEven); // ์ถ๋ ฅ: 2
includes(): ๋ฐฐ์ด์ ํน์ ๊ฐ์ด ์๋์ง ์ฌ๋ถ๋ฅผ ๋ฐํ
let arr = [1, 2, 3];
console.log(arr.includes(2)); // ์ถ๋ ฅ: true
3.5 ์ ๋ ฌ
sort(): ๋ฐฐ์ด์ ์ ๋ ฌ (๊ธฐ๋ณธ์ ์ผ๋ก ์์๋ค์ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ์ฌ์ ์์ผ๋ก ์ ๋ ฌ)
let arr = [3, 1, 4, 1, 5, 9];
arr.sort();
console.log(arr); // ์ถ๋ ฅ: [1, 1, 3, 4, 5, 9]
// ์ซ์๋ฅผ ์ ๋๋ก ์ ๋ ฌํ๋ ค๋ฉด ๋น๊ตํจ์๋ฅผ ์ ๋ฌ
arr.sort((a, b) => a - b);
console.log(arr); // ์ถ๋ ฅ: [1, 1, 3, 4, 5, 9]
4. ๋ค์ฐจ์ ๋ฐฐ์ด
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(matrix[1][1]); // ์ถ๋ ฅ: 5
5. ๋ฐฐ์ด์ ๊ธธ์ด
length
let arr = [1, 2, 3, 4, 5];
console.log(arr.length); // ์ถ๋ ฅ: 5
๐ ๊ฐ์ฒด(Object)
ํค-๊ฐ ์์ ์งํฉ์ผ๋ก, ๋ค์ํ ์์ฑ(property)๊ณผ ๋ฉ์๋(method)๋ฅผ ๊ฐ์ง ์ ์๋ ๋ณตํฉ ๋ฐ์ดํฐ ๊ตฌ์กฐ
1. ๊ฐ์ฒด ์์ฑ
- ๊ฐ์ฒด ๋ฆฌํฐ๋ด
์ค๊ดํธ {} ์์ ์์ฑ์ ์ ์
let person = {
name: "Alice",
age: 25,
isStudent: true
};
2. new Object() ์ฌ์ฉ
new Object()๋ฅผ ์ฌ์ฉํด ๊ฐ์ฒด๋ฅผ ์์ฑ
let person = new Object();
person.name = "Alice";
person.age = 25;
person.isStudent = true;
2. ๊ฐ์ฒด์ ์์ฑ
๊ฐ์ฒด๋ ์์ฑ(property)์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ ํค(key)์ ๊ฐ(value)์ ์์ผ๋ก ๊ตฌ์ฑ
์์ฑ ์ ๊ทผ ๋ฐฉ๋ฒ:
- ์ ํ๊ธฐ๋ฒ (dot notation):
- console.log(person.name); // "Alice" console.log(person.age); // 25
- ๋๊ดํธ ํ๊ธฐ๋ฒ (bracket notation): ๋์ ์ผ๋ก ์์ฑ๋ช ์ ์ ์ํ ๋ ์ ์ฉ
-
let key = "name"; console.log(person[key]); // "Alice" - console.log(person["name"]); // "Alice" console.log(person["age"]); // 25
์์ฑ ์ถ๊ฐ/์์ /์ญ์ :
- ์์ฑ ์ถ๊ฐ/์์ :
- person.gender = "female"; // ์๋ก์ด ์์ฑ ์ถ๊ฐ person.age = 26; // ๊ธฐ์กด ์์ฑ ์์
- ์์ฑ ์ญ์ :
- delete person.isStudent; console.log(person); // { name: "Alice", age: 26, gender: "female" }
3. ๊ฐ์ฒด์ ๋ฉ์๋
๊ฐ์ฒด๋ ๋ฉ์๋(method)๋ผ๊ณ ๋ถ๋ฆฌ๋ ํจ์ ํฌํจ → ๊ฐ์ฒด์ ๋์(๊ธฐ๋ฅ)์ ์ ์
let person = {
name: "Alice",
age: 25,
greet: function() {
console.log(`Hello, my name is ${this.name}.`);
}
};
person.greet(); // ์ถ๋ ฅ: Hello, my name is Alice.
- this ํค์๋: ๊ฐ์ฒด ๋ด๋ถ์ ๋ฉ์๋์์ this๋ ํด๋น ๊ฐ์ฒด → this.name์ person.name์ ์ฐธ์กฐ
4. ๊ฐ์ฒด ๋ฐ๋ณต
๊ฐ์ฒด์ ์์ฑ์ ๋ฐ๋ณต(iterate)ํ๋ ค๋ฉด for...in ๋ฌธ ์ฌ์ฉ
let person = {
name: "Alice",
age: 25,
isStudent: true
};
for (let key in person) {
console.log(`${key}: ${person[key]}`);
}
// ์ถ๋ ฅ:
// name: Alice
// age: 25
// isStudent: true
5. ๊ฐ์ฒด ๊ด๋ จ ๋ฉ์๋
- Object.keys()
๊ฐ์ฒด์ ๋ชจ๋ ํค๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ
let person = { name: "Alice", age: 25, gender: "female" };
console.log(Object.keys(person)); // ์ถ๋ ฅ: ["name", "age", "gender"]
2. Object.values()
๊ฐ์ฒด์ ๋ชจ๋ ๊ฐ์ ๋ฐฐ์ด๋ก ๋ฐํ
console.log(Object.values(person)); // ์ถ๋ ฅ: ["Alice", 25, "female"]
3. Object.entries()
๊ฐ์ฒด์ ํค-๊ฐ ์์ ๋ฐฐ์ด์ ํํ๋ก ๋ฐํ
console.log(Object.entries(person));
// ์ถ๋ ฅ: [["name", "Alice"], ["age", 25], ["gender", "female"]]
4. Object.assign()
ํ๋์ ๊ฐ์ฒด์์ ๋ค๋ฅธ ๊ฐ์ฒด๋ก ์์ฑ์ ๋ณต์ฌ
let additionalInfo = { city: "Seoul", country: "South Korea" };
Object.assign(person, additionalInfo);
console.log(person);
// ์ถ๋ ฅ: { name: "Alice", age: 25, gender: "female", city: "Seoul", country: "South Korea" }
7. ๊ฐ์ฒด ๋ณต์ฌ
๊ฐ์ฒด๋ ์ฐธ์กฐ ํ์ → ๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ ๋ณ์์ ํ ๋นํ๋ฉด ์๋ณธ ๊ฐ์ฒด์ ๋ํ ์ฐธ์กฐ๊ฐ ๋ณต์ฌ
- ์์ ๋ณต์ฌ (shallow copy):
- *Object.assign()*์ ์ฌ์ฉํ๊ฑฐ๋, ์คํ๋ ๋ ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ ์์ ๋ณต์ฌ
let obj1 = { a: 1, b: 2 }; let obj2 = Object.assign({}, obj1); // ๋๋ let obj2 = { ...obj1 }; obj2.a = 3; console.log(obj1.a); // ์ถ๋ ฅ: 1 (์๋ณธ์ ๋ณํ์ง ์์) - ๊น์ ๋ณต์ฌ (deep copy):
- ๊น์ ๋ณต์ฌ๋ ๊ฐ์ฒด์ ์ค์ฒฉ๋ ๊ฐ๋ค๊น์ง ๋ชจ๋ ๋ณต์ฌํ๋ ๋ฐฉ๋ฒ. ์ด๋ฅผ ์ํด์๋ ์ฌ๊ท ํจ์๋ JSON ๋ฉ์๋๋ฅผ ์ฌ์ฉ
let obj1 = { a: 1, b: { c: 2 } }; let obj2 = JSON.parse(JSON.stringify(obj1)); obj2.b.c = 3; console.log(obj1.b.c); // ์ถ๋ ฅ: 2 (๊น์ ๋ณต์ฌ๋ก ์๋ณธ์ ๋ณํ์ง ์์)
'Dev > Language' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Java] Maven๊ณผ Gradle (1) | 2024.12.28 |
|---|---|
| [Java] ์ฐ์ฐ์, ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ (0) | 2024.10.01 |
| [Java] ๋ณ์, ์์, ์๋ฃํ (2) | 2024.09.24 |
| [JavaScript] ๋ณ์ ์ ์ธ | ๋ฐ์ดํฐ ํ์ | ์ฐ์ฐ์ | ์กฐ๊ฑด๋ฌธ | ๋ฐ๋ณต๋ฌธ (0) | 2024.09.24 |
| [JavaScript] ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์ ์ถ๋ ฅํ๋ ๋ฒ์ ์์๋ณด์ ๐คหห (0) | 2024.07.02 |