html
JavaScript์์ Methods์ Functions ๋ง์คํฐํ๊ธฐ: ํฌ๊ด์ ์ธ ๊ฐ์ด๋
๋ชฉ์ฐจ
- ์๊ฐ .......................................................... 1
- JavaScript์์ Functions ์ดํดํ๊ธฐ ..... 3
- Functions๋? .................................... 3
- Arrow Functions ๋ Regular Functions ....................................................... 5
- Functions ํธ์ถํ๊ธฐ ........................................ 7
- Methods ์ฌํ ............................................ 9
- Methods๋? .......................................... 9
- Functions์ Methods์ ์ฐจ์ด ........................................................ 11
- ์ค์ฉ์ ์ธ ์์ ............................................ 13
- Functions ์์ฑ ๋ฐ ์ฌ์ฉํ๊ธฐ ................... 13
- Objects์ Methods ์ ์ฉํ๊ธฐ .................... 15
- ๊ฒฐ๋ก ............................................................... 17
์๊ฐ
"JavaScript์์ Methods์ Functions ๋ง์คํฐํ๊ธฐ"์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค. ์ด ๊ฐ์ด๋๋ JavaScript ํ๋ก๊ทธ๋๋ฐ์์ functions์ methods์ ํ์ ์ดํดํ๊ณ ํ์ฉํ ์ ์๋๋ก ๋์์ค๋๋ค. ๊ธฐ์ด๋ฅผ ์ดํดํ๊ณ ์ ํ๋ ์ด๋ณด์๋ ์ฝ๋ฉ ์ค๋ ฅ์ ํฅ์์ํค๋ ค๋ ๊ฐ๋ฐ์๋ , ์ด eBook์ ์ด๋ฌํ ํ์ ๊ฐ๋ ์ ๋ํ ๋ช ํํ๊ณ ๊ฐ๊ฒฐํ ํ๊ตฌ๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ด ๊ฐ์ด๋์์๋ ๋ค์์ ๋ค๋ฃฐ ๊ฒ์ ๋๋ค:
- functions์ methods์ ์ ์ ๋ฐ ๋ชฉ์ .
- arrow functions๊ณผ regular functions์ ์ฐจ์ด.
- ํจ์๋ฅผ ์์ฑํ๊ณ ํธ์ถํ๋ ๋ฐฉ๋ฒ.
- methods์ ์ดํด์ objects์์ ์ ์ฉ.
- ์์ธํ ์ค๋ช ๊ณผ ์ฝ๋ ์ค๋ํซ์ ํฌํจํ ์ค์ฉ์ ์ธ ์์ .
์ด eBook์ ๋๊น์ง ์ฝ์ผ๋ฉด JavaScript ํ๋ก์ ํธ์์ functions์ methods๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉํ๋ ๋ฐ ํ์ํ ํํํ ๊ธฐ์ด๋ฅผ ๊ฐ์ถ๊ฒ ๋ ๊ฒ์ ๋๋ค.
JavaScript์์ Functions ์ดํดํ๊ธฐ
Functions๋?
Functions๋ JavaScript์ ํต์ฌ ๋น๋ฉ ๋ธ๋ก ์ค ํ๋์ ๋๋ค. ํน์ ์์ ์ ์ํํ๋๋ก ์ค๊ณ๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ฝ๋ ๋ธ๋ก์ ๋๋ค. Functions๋ ์ฝ๋๋ฅผ DRY(Don't Repeat Yourself)ํ๊ฒ ์ ์งํ๊ณ ์กฐ์งํํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
Functions์ ์ฃผ์ ํน์ฑ:
- ์ฌ์ฌ์ฉ์ฑ: ํ ๋ฒ ์์ฑํ๊ณ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉ.
- ๋ชจ๋์ฑ: ๋ณต์กํ ๋ฌธ์ ๋ฅผ ๊ด๋ฆฌ ๊ฐ๋ฅํ ๋ถ๋ถ์ผ๋ก ๋ถํด.
- ์ ์ง๋ณด์์ฑ: ๋๋ฒ๊น ๋ฐ ์ ๋ฐ์ดํธ๊ฐ ์ฉ์ด.
Function์ ๊ธฐ๋ณธ ๊ตฌ๋ฌธ:
1 2 3 4 5 |
function functionName(parameters) { // Function body return result; } |
Arrow Functions ๋ Regular Functions
JavaScript๋ functions๋ฅผ ์ ์ํ๋ ๋ ๊ฐ์ง ์ฃผ์ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค: regular functions๊ณผ arrow functions. ์ด๋ค ๊ฐ์ ์ฐจ์ด๋ฅผ ์ดํดํ๋ ๊ฒ์ ํจ์จ์ ์ด๊ณ ์ฝ๊ธฐ ์ฌ์ด ์ฝ๋๋ฅผ ์์ฑํ๋ ๋ฐ ์ค์ํฉ๋๋ค.
Regular Functions
Regular functions๋ function ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ์๋ฉ๋๋ค.
์์ :
1 2 3 4 |
function add(a, b) { return a + b; } |
ํน์ฑ:
- Hoisting: ์ฝ๋์์ ์ ์๋๊ธฐ ์ ์ ํธ์ถํ ์ ์์ต๋๋ค.
- Context (this): ํธ์ถ๋๋ ๋ฐฉ์์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋ฉ๋๋ค.
Arrow Functions
Arrow functions๋ ๋ ๊ฐ๊ฒฐํ ๊ตฌ๋ฌธ์ ์ ๊ณตํ๋ฉฐ regular functions์ ๋ค๋ฅธ ๋์์ ๊ฐ์ง๋๋ค.
์์ :
1 2 3 4 |
const add = (a, b) => { return a + b; }; |
ํน์ฑ:
- Hoisting ์์: ์ ์๋๊ธฐ ์ ์ ํธ์ถํ ์ ์์ต๋๋ค.
- Lexical this: ์ฃผ๋ณ ์ค์ฝํ์์ this๋ฅผ ์์๋ฐ์ต๋๋ค.
๋น๊ต ํ: Arrow Functions ๋ Regular Functions
ํน์ง | Regular Functions | Arrow Functions |
---|---|---|
๊ตฌ๋ฌธ | function functionName() {} |
const functionName = () => {} |
Hoisting | ์ | ์๋์ค |
this Context | ๋์ | Lexical (์์๋จ) |
Constructor ์ฌ์ฉ | ์์ฑ์๋ก ์ฌ์ฉ ๊ฐ๋ฅ | ์์ฑ์๋ก ์ฌ์ฉ ๋ถ๊ฐ |
arguments ๊ฐ์ฒด |
์ฌ์ฉ ๊ฐ๋ฅ | ์ฌ์ฉ ๋ถ๊ฐ |
๊ฐ๊ฒฐ์ฑ | ๋ ์ฅํฉํจ | ๋ ๊ฐ๊ฒฐํจ |
Functions ํธ์ถํ๊ธฐ
Function์ ํธ์ถํ๋ ๊ฒ์ ์ด๋ฆ์ผ๋ก ํธ์ถํ๊ณ ํ์ํ ์ธ์๋ฅผ ์ ๋ฌํ๋ ๊ฒ์ ํฌํจํฉ๋๋ค.
์์ :
1 2 3 4 5 6 |
function greet(name) { return `Hello, ${name}!`; } console.log(greet('Alice')); // Output: Hello, Alice! |
Functions ์ง์ ํธ์ถํ๊ธฐ:
Functions๋ ๋ค๋ฅธ functions๋ methods ๋ด์์ ์ง์ ํธ์ถํ ์ ์์ต๋๋ค.
์์ :
1 2 3 4 5 6 |
function multiply(a, b) { return a * b; } console.log(multiply(10, 35)); // Output: 350 |
Methods ์ฌํ
Methods๋?
Methods๋ objects์ ์ฐ๊ด๋ functions์ ๋๋ค. ์ด๋ค์ ์์ ์ด ์ํ object์ ๋ํด ์ํํ ์ ์๋ ๋์์ ๋ํ๋ ๋๋ค.
์์ :
1 2 3 4 5 6 7 8 9 |
const user = { name: 'John Doe', greet: function() { return `Hello, ${this.name}!`; } }; console.log(user.greet()); // Output: Hello, John Doe! |
Functions์ Methods์ ์ฐจ์ด
Functions์ Methods๋ ํน์ ์์ ์ ์ํํ๋๋ก ์ค๊ณ๋ ์ฝ๋ ๋ธ๋ก์ด๋ผ๋ ์ ์์ ์ ์ฌํ์ง๋ง, ์ฃผ์ ์ฐจ์ด๋ ๊ทธ๋ค์ ์ฐ๊ด์ฑ๊ณผ ํธ์ถ ๋ฐฉ์์ ์์ต๋๋ค.
์ธก๋ฉด | Function | Method |
---|---|---|
์ฐ๊ด์ฑ | ๋ ๋ฆฝ์ , ์ด๋ค object์๋ ๋ฐ์ธ๋ฉ๋์ง ์์ | object์ ์ฐ๊ด๋จ |
ํธ์ถ | ์ด๋ฆ์ผ๋ก ์ง์ ํธ์ถ | object์ ์ ํ๊ธฐ๋ฒ์ ์ฌ์ฉํ์ฌ ํธ์ถ |
Context (this) | Function ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋ฌ๋ผ์ง | ๋๊ฐ method๊ฐ ์ํ object๋ฅผ ์ฐธ์กฐ |
์ฌ์ฉ ์ฌ๋ก | ์ผ๋ฐ์ ์ธ ์์ | ๊ด๋ จ๋ object์ ํน์ ํ ์์ |
Method์ Function์ ์์ :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Function function add(a, b) { return a + b; } // Method const calculator = { add: function(a, b) { return a + b; } }; console.log(add(5, 10)); // Function ํธ์ถ console.log(calculator.add(5, 10)); // Method ํธ์ถ |
์ค์ฉ์ ์ธ ์์
Functions ์์ฑ ๋ฐ ์ฌ์ฉํ๊ธฐ
JavaScript์์ arrow functions์ regular functions๋ฅผ ์์ฑํ๊ณ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
Arrow Function ์์
1 2 3 4 5 6 7 8 |
// Arrow Function const add = (a, b) => { return a + b; }; // Arrow function ์ฌ์ฉํ๊ธฐ console.log(add(10, 35)); // Output: 45 |
์ค๋ช :
- ์ ์:
add
ํจ์๋ ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์a
์b
๋ฅผ ๋ฐ๋ arrow function ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ ์๋์์ต๋๋ค. - Return ๋ฌธ:
a
์b
์ ํฉ์ ๋ฐํํฉ๋๋ค. - ํธ์ถ: ํจ์๋
10
๊ณผ35
๋ผ๋ ์ธ์๋ก ํธ์ถ๋๋ฉฐ, ๊ฒฐ๊ณผ๊ฐ ์ฝ์์ ๊ธฐ๋ก๋ฉ๋๋ค.
Regular Function ๋ณํ
์์ arrow function์ regular function์ผ๋ก ๋ณํํ ์ ์์ต๋๋ค:
1 2 3 4 5 6 7 8 |
// Regular Function function add(a, b) { return a + b; } // Regular function ์ฌ์ฉํ๊ธฐ console.log(add(10, 35)); // Output: 45 |
์ฃผ์ ๋ณ๊ฒฝ ์ฌํญ:
- arrow ๊ตฌ๋ฌธ์ function ํค์๋๋ก ๊ต์ฒดํ์ต๋๋ค.
const
์ ์ธ์ ์ ๊ฑฐํ์ต๋๋ค.
Objects์ Methods ์ ์ฉํ๊ธฐ
Methods๋ฅผ ์ฌ์ฉํ๋ฉด objects ๋ด์ ์บก์ํ๋ ๋ฐ์ดํฐ์ ๋ํด ์์ ์ ์ํํ ์ ์์ต๋๋ค. ๋ฌธ์์ด ์กฐ์์ ์์ ๋ก ์ดํด๋ณด๊ฒ ์ต๋๋ค.
1 2 3 4 5 6 7 |
const name = 'alice'; // ๋ฌธ์์ด object์ toUpperCase method ์ฌ์ฉํ๊ธฐ const upperName = name.toUpperCase(); console.log(upperName); // Output: ALICE |
์ค๋ช :
- String Object:
name
์ ๋ฌธ์์ด ํ๋ฆฌ๋ฏธํฐ๋ธ์ ๋๋ค. JavaScript์์ ๋ฌธ์์ด ํ๋ฆฌ๋ฏธํฐ๋ธ๋ methods๊ฐ ํธ์ถ๋ ๋ ์๋์ผ๋ก String ๊ฐ์ฒด๋ก ๋ํ๋ฉ๋๋ค. - Method ํธ์ถ:
toUpperCase
method๊ฐname
์ ํธ์ถ๋์ด ๋๋ฌธ์๋ก ๋ณํ๋ฉ๋๋ค. - ๊ฒฐ๊ณผ: ๋๋ฌธ์๋ก ๋ณํ๋ ๋ฌธ์์ด์ด
upperName
์ ์ ์ฅ๋๊ณ ์ฝ์์ ๊ธฐ๋ก๋ฉ๋๋ค.
์ฝ๋ ๋จ๊ณ๋ณ ๋ถ์
1 2 3 4 5 6 7 8 |
const name = 'alice'; // ๋ฌธ์์ด ๋ณ์ ์ ์ const upperName = name.toUpperCase(); // 'name' ๋ฌธ์์ด์ toUpperCase method ํธ์ถ // method๋ ๋๋ฌธ์๋ก ๋ ์ ๋ฌธ์์ด์ ๋ฐํํ๊ณ 'upperName'์ ํ ๋น console.log(upperName); // ๊ฒฐ๊ณผ ์ถ๋ ฅ: ALICE |
Functions์ Methods ๊ฒฐํฉํ๊ธฐ
Functions์ Methods๋ฅผ ํ๋์ ์์ ์ ๊ฒฐํฉํ์ฌ ์ด๋ป๊ฒ ์ํธ ์์ฉํ๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค.
1 2 3 4 5 6 7 8 9 10 |
// ๋ ๋ฌธ์์ด์ ์ฐ๊ฒฐํ๋ Function function concatenate(a, b) { return a + b; } // ์ฐ๊ฒฐ๋ ๋ฌธ์์ด์ ๋๋ฌธ์๋ก ๋ณํํ๋ Method const result = concatenate('hello, ', 'world').toUpperCase(); console.log(result); // Output: HELLO, WORLD |
์ค๋ช :
- Function ์ ์:
concatenate
ํจ์๋ ๋ ๊ฐ์ ๋ฌธ์์ด์ ๋ฐ์ ์ด๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค. - Function ํธ์ถ ๋ฐ Method ํธ์ถ: ํจ์๋
'hello, '
์'world'
๋ผ๋ ์ธ์๋ก ํธ์ถ๋๊ณ , ๊ฒฐ๊ณผ ๋ฌธ์์ด์ ์ฆ์toUpperCase
method์ ์ ๋ฌ๋ฉ๋๋ค. - ๊ฒฐ๊ณผ: ์ต์ข
๋๋ฌธ์ ๋ฌธ์์ด
'HELLO, WORLD'
๊ฐ ์ฝ์์ ๊ธฐ๋ก๋ฉ๋๋ค.
๊ฒฐ๋ก
์ด ํฌ๊ด์ ์ธ ๊ฐ์ด๋์์๋ JavaScript์์ functions์ methods์ ๊ธฐ๋ณธ ๊ฐ๋ ์ ํ๊ตฌํ์ต๋๋ค. ๋ ๋ฆฝ์ ์ธ functions์ object์ ์ฐ๊ด๋ methods์ ์ฐจ์ด๋ฅผ ์ดํดํ๋ ๊ฒ์ ๊นจ๋ํ๊ณ ํจ์จ์ ์ด๋ฉฐ ์ ์ง๋ณด์ ๊ฐ๋ฅํ ์ฝ๋๋ฅผ ์์ฑํ๋ ๋ฐ ์ค์ํฉ๋๋ค.
์ฃผ์ ์์ฝ:
- Functions๋ ํน์ ์์ ์ ์ํํ๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ฝ๋ ๋ธ๋ก์ผ๋ก, regular ๋๋ arrow ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ ์ํ ์ ์์ต๋๋ค.
- Methods๋ objects์ ์ฐ๊ด๋ functions๋ก, object์ ๋ฐ์ดํฐ์ ๋ํด ์์ ์ ์ํํ ์ ์์ต๋๋ค.
- Arrow Functions๋ ๋ ๊ฐ๊ฒฐํ ๊ตฌ๋ฌธ์ ์ ๊ณตํ๋ฉฐ, lexical this ๋ฐ์ธ๋ฉ์ ๊ฐ์ง๊ณ ์์ด ํน์ ์๋๋ฆฌ์ค์ ์ ํฉํฉ๋๋ค.
- Regular Functions๋ this ์ปจํ ์คํธ์ ์์ฑ์ ์ฌ์ฉ์ ๋ ๋ง์ ์ ์ฐ์ฑ์ ์ ๊ณตํ์ฌ ๋ค์ํ ์ํฉ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด๋ฌํ ๊ฐ๋ ์ ๋ง์คํฐํจ์ผ๋ก์จ JavaScript ํ๋ก๊ทธ๋๋ฐ ์ค๋ ฅ์ ํฅ์์์ผ ๋ณด๋ค ๊ฒฌ๊ณ ํ๊ณ ํ์ฅ ๊ฐ๋ฅํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ ์ ์์ต๋๋ค.
์ฐธ๊ณ : ์ด ๊ฒ์๋ฌผ์ AI์ ์ํด ์์ฑ๋์์ต๋๋ค.