JavaScript Arrow Functions vs JavaScript Functions
- English ๐บ๐ธ
- ํ๊ตญ์ด ๐ฐ๐ท
When to Use Eachโ
Use traditional functionsโ
- When you need to use function hoisting (arrow functions are not hoisted).
- If you need to access the
arguments
object. - When using methods that will be added to an object's prototype.
- If you need a dynamic context (
this
), such as in event handlers.
Use arrow functionsโ
- For shorter syntax in functional programming scenarios.
- When working with higher-order functions that expect a function callback.
- In cases where you want to retain the lexical
this
scope (e.g., in nested functions or event handlers). - For single-line functions with implicit returns.
Differencesโ
Syntaxโ
Arrow functions provide a shorter syntax. They are written with an arrow rather than the function
keyword.
Traditional functionโ
function add(a, b) {
return a + b
}
Arrow function:โ
const add = (a, b) => a + b;
this
Bindingโ
One of the most significant differences is how this
is handled. In traditional functions, this
is dynamic and can change depending on the context in which the function is called. In arrow functions, this
is lexically scoped, meaning it uses this
from the surrounding code where the function is defined.
No Binding of arguments
โ
Arrow functions do not have their own arguments
object. Instead, they access the arguments
object of the closest non-arrow parent function.
Cannot be used as Constructorsโ
Arrow functions cannot be used as constructor functions. They cannot be called with new
.
No Duplicate Named Parametersโ
Traditional functions allow duplicate named parameters in non-strict mode, while arrow functions do not, regardless of strict mode.
No prototype
Propertyโ
Arrow functions do not have a prototype
property.
Implicit Returnโ
In arrow functions, if you have a single expression, you can omit the curly braces {}
and the return
statement. The expression automatically returns its result.
๊ฐ๊ฐ ์ฌ์ฉํ ๋โ
์ ํต์ ์ธ ํจ์ ์ฌ์ฉโ
- ํจ์ ํธ์ด์คํ ์ ์ฌ์ฉํด์ผ ํ ๋ (ํ์ดํ ํจ์๋ ํธ์ด์คํ ๋์ง ์๋๋ค).
arguments
๊ฐ์ฒด์ ์ ๊ทผํด์ผ ํ ๋.- ๊ฐ์ฒด์ ํ๋กํ ํ์ ์ ์ถ๊ฐ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋.
- ๋์ ๋งฅ๋ฝ(
this
)์ด ํ์ํ ๋, ์๋ฅผ ๋ค์ด ์ด๋ฒคํธ ํธ๋ค๋ฌ์์.
ํ์ดํ ํจ์ ์ฌ์ฉโ
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ํฉ์์ ๋ ์งง์ ๋ฌธ๋ฒ์ด ํ์ํ ๋.
- ํจ์ ์ฝ๋ฐฑ์ ๊ธฐ๋ํ๋ ๊ณ ์ฐจ ํจ์์ ํจ๊ป ์์ ํ ๋.
- ์ดํ์
this
๋ฒ์๋ฅผ ์ ์งํ๊ณ ์ ํ ๋ (์: ์ค์ฒฉ ํจ์๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ์์). - ์์์ ๋ฐํ์ ๊ฐ์ง ๋จ์ผ ์ค ํจ์์ ๋ํด.
์ฐจ์ด์ โ
๋ฌธ๋ฒโ
ํ์ดํ ํจ์๋ ๋ ์งง์ ๋ฌธ๋ฒ์ ์ ๊ณตํ๋ค. function
ํค์๋ ๋์ ํ์ดํ๋ก ์์ฑ๋๋ค.
์ ํต์ ์ธ ํจ์โ
function add(a, b) {
return a + b
}
ํ์ดํ ํจ์:โ
const add = (a, b) => a + b;
this
๋ฐ์ธ๋ฉโ
๊ฐ์ฅ ์ค์ํ ์ฐจ์ด์ ์ค ํ๋๋ this
๊ฐ ์ด๋ป๊ฒ ์ฒ๋ฆฌ๋๋์ง์ ์๋ค. ์ ํต์ ์ธ ํจ์์์๋ this
๊ฐ ๋์ ์ด๋ฉฐ ํจ์๊ฐ ํธ์ถ๋๋ ๋งฅ๋ฝ์ ๋ฐ๋ผ ๋ณ๊ฒฝ๋ ์ ์๋ค. ํ์ดํ ํจ์์์๋ this
๊ฐ ์ดํ์ ์ผ๋ก ๋ฒ์๊ฐ ์ง์ ๋๋ฉฐ, ํจ์๊ฐ ์ ์๋ ์ฃผ๋ณ ์ฝ๋์ this
๋ฅผ ์ฌ์ฉํ๋ค.
arguments
๋ฐ์ธ๋ฉ ์์โ
ํ์ดํ ํจ์๋ ์์ฒด arguments
๊ฐ์ฒด๊ฐ ์๋ค. ๋์ , ๊ฐ์ฅ ๊ฐ๊น์ด ๋น-ํ์ดํ ๋ถ๋ชจ ํจ์์ arguments
๊ฐ์ฒด์ ์ ๊ทผํ๋ค.
์์ฑ์๋ก ์ฌ์ฉ ๋ถ๊ฐโ
ํ์ดํ ํจ์๋ ์์ฑ์ ํจ์๋ก ์ฌ์ฉํ ์ ์๋ค. new
์ ํจ๊ป ํธ์ถํ ์ ์๋ค.
์ค๋ณต๋ ์ด๋ฆ์ ๋งค๊ฐ๋ณ์ ๊ธ์งโ
์ ํต์ ์ธ ํจ์๋ ๋น์๊ฒฉ ๋ชจ๋์์ ์ค๋ณต๋ ์ด๋ฆ์ ๋งค๊ฐ๋ณ์๋ฅผ ํ์ฉํ์ง๋ง, ํ์ดํ ํจ์๋ ์๊ฒฉ ๋ชจ๋์ ๊ด๊ณ์์ด ์ด๋ฅผ ํ์ฉํ์ง ์๋๋ค.
prototype
์์ฑ ์์โ
ํ์ดํ ํจ์๋ prototype
์์ฑ์ด ์๋ค.
์์์ ๋ฐํโ
ํ์ดํ ํจ์์์๋ ๋จ์ผ ํํ์์ ์ฌ์ฉํ ๊ฒฝ์ฐ, ์ค๊ดํธ {}
์ return
๋ฌธ์ ์๋ตํ ์ ์๋ค. ํํ์์ ์๋์ผ๋ก ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค.