Function μ—°μŠ΅ν€΄μ¦ˆ 3

1. ν•¨μˆ˜μ—μ„œ typeof μ‚¬μš©
λ§€κ°œλ³€μˆ˜μ˜ νƒ€μž…μ„ λ°˜ν™˜ν•˜λŠ” getType ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
typeof μ—°μ‚°μž μ‚¬μš©
2. ν•¨μˆ˜μ—μ„œ isNaN μ‚¬μš©
값이 NaN이면 trueλ₯Ό λ°˜ν™˜ν•˜λŠ” isValueNaN ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
isNaN ν•¨μˆ˜ μ‚¬μš©
3. ν•¨μˆ˜μ—μ„œ arguments 객체 μ‚¬μš©
λͺ¨λ“  인수의 곱을 λ°˜ν™˜ν•˜λŠ” multiplyAll ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
arguments 객체 λ˜λŠ” ...args μ‚¬μš©
4. ν•¨μˆ˜μ—μ„œ μž¬κ·€ μ‚¬μš©
n! (νŒ©ν† λ¦¬μ–Ό)을 λ°˜ν™˜ν•˜λŠ” factorial ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
if(n === 0) return 1; μž¬κ·€ 호좜 μ‚¬μš©
5. ν•¨μˆ˜μ—μ„œ 쀑첩 ν•¨μˆ˜ μ‚¬μš©
ν•¨μˆ˜ μ•ˆμ— ν•¨μˆ˜κ°€ μžˆλŠ” outer ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜κ³ , inner ν•¨μˆ˜μ—μ„œ 'Hello'λ₯Ό 좜λ ₯ν•˜κ²Œ ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
ν•¨μˆ˜ μ•ˆμ— function inner() { ... } μž‘μ„±
6. ν•¨μˆ˜μ—μ„œ ν΄λ‘œμ € μ‚¬μš©
addMaker ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ—¬ add5 = addMaker(5); add5(3) μ‹€ν–‰ μ‹œ 8이 λ‚˜μ˜€κ²Œ ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
return function(y) { return x + y; }
7. ν•¨μˆ˜μ—μ„œ μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜(IIFE) μ‚¬μš©
μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜λ‘œ 'Hello'λ₯Ό 좜λ ₯ν•˜λŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
(function() { ... })(); ν˜•νƒœ μ‚¬μš©
8. ν•¨μˆ˜μ—μ„œ this μ‚¬μš©
객체 obj에 getName λ©”μ„œλ“œλ₯Ό μΆ”κ°€ν•˜μ—¬ this.name을 λ°˜ν™˜ν•˜κ²Œ ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
obj.getName = function() { return this.name; }
9. ν•¨μˆ˜μ—μ„œ new μ—°μ‚°μž μ‚¬μš©
μƒμ„±μž ν•¨μˆ˜ Person을 λ§Œλ“€μ–΄ name을 ν”„λ‘œνΌν‹°λ‘œ μ €μž₯ν•˜κ²Œ ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
function Person(name) { this.name = name; }
10. ν•¨μˆ˜μ—μ„œ call/apply μ‚¬μš©
ν•¨μˆ˜ showName을 obj의 this둜 μ‹€ν–‰ν•˜μ—¬ 'Tom'을 좜λ ₯ν•˜κ²Œ ν•˜μ‹œμ˜€.
// 여기에 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”
showName.call(obj) λ˜λŠ” showName.apply(obj)