โ ์ผ๋ฐ ํจ์
this
๋ ๋ง์ง๋ง์ผ๋ก ์์ ์ ํ๊ณ ์๋ scope๋ก ํญ์ ๋ณํจ (โ์ด๋์ ์ ์ธํ๋๋โ โญ์ด๋์ ํธ์ถํ๋๋โญ)
const example = {
name: "world",
speak: function() {
function getName() {
return `${this.name}`;
}
return `Hello ${getName()}`;
}
};
example.speak(); // Hello
//์ด๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด..
const example = {
name: "world",
speak: function() {
//์ฌ๊ธฐ๊น์ง๋ this์ ๊ฐ์ฒด example์ด bind๋ผ์์
const self = this; // self๋ฅผ ์ ์ธํด์ ๊ฒฐํฉ์ํด
function getName() {
return `${self.name}`;
}
return `Hello ${getName()}`;
}
};
example.speak(); // Hello world
โ ํ์ดํํจ์ (Arrow Function)
- ES6์ ๋ฑ์ฅ^^...ใ ใ
- ์์ ์ปจํ
์คํธ๊ฐ ์ ์ญ =
window
() ⇒ {}
๋ชจ์์ ํ๊ณ ์์self
์์จ๋ ๋จ (์ฒ์ ๋ฐ์ธ๋ฉ ๋ ์ค์ฝํ ์์์ ๊ฐ๋ฆฌํค๋ this๊ฐ ๋ณํ์ง ์์)
//์๋ ๊ฐ์ ์ฝ๋์
const example = {
name: "world",
speak: function() {
const getName = () => {
return `${this.name}`; //this๋ฅผ ์ ์ (lexically)์ผ๋ก ๊ฒฐํฉ์ํด
}
return `Hello ${getName()};
}
};
example.speak(); // Hello world
๐จ ํ์ดํ ํจ์ ์ธ ๋ ์ฃผ์์ !
prototype
์์ฑ์ด ์์โ- ์์ฑ์์ new ํค์๋์ ๊ฐ์ด ์ฌ์ฉ๋ ์ ์์โ
- ๋ฉ์๋๋ก ์ฌ์ฉํ๋ฉด ์๋จโ
- addEventListener ํจ์์ callback ํจ์๋ก ์ฌ์ฉํ๋ฉด ์๋จโ (๋ด๋ถ์์ this๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ)
๐ค ๊ทธ๋ฆฌ๊ณ ๋ ๋ค๋ฅธ ์ฐจ์ด์ ์?
- ๊ธฐ์กด์ function์ ํจ์์ ์ด๋ฆ ์์ฒด๋ฅผ ์ง์ ํด ์ค ์ ์์ง๋ง,
ํ์ดํ ํจ์๋ ๋ณ์์ ๋ด์์ ์ฌ์ฉํด์ผ ํจ (ํจ์ ์์ฒด์ ํธ์ด์คํ ๋ถ๊ฐ)
function example() { console.log("function"); }
const example = () => { console.log("arrow function"); }
- ๊ธฐ์กด์ ํจ์๋ arguments๋ฅผ ์ฌ์ฉํด์ ๋ฐ์์ค๋ ์ธ์๋ค์ ๋ฐฐ์ด๋ก ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉ ๊ฐ๋ฅํ์ผ๋,
ํ์ดํ ํจ์์์๋ argument ๋์rest parameter
์ฌ์ฉ
๐ก arguments
arguments ๊ฐ์ฒด๋ ํจ์์ ์ ๋ฌ๋๋ ์ธ์์ ๋ฐฐ์ด ์งํฉ. ์๋์ ๊ฐ์ด ๋ฐฐ์ด๋ก ๋ณํ์ด ๊ฐ๋ฅํจ.let args = Array.prototype.slice.call(arguments);
const funcName = (...arg) => { console.log(arg) } // [1, 2, 3]
funcName(1, 2, 3);
๐ ๊ฐ์ด ๋ณด๋ฉด ์ข์์ง๋ ๋ชจ๋ฅด๋ ๊ธ
[JavaScript] JS์ Implicit Return (์์์ ๋ฐํ)
๐์ฐธ๊ณ (๊ฐ์ฌํฉ๋๋ค)
https://kim-solshar.tistory.com/57
[์๋ฐ์คํฌ๋ฆฝํธ] arrow function๊ณผ this
ecmascript6๊ฐ ๋์จ์ง๋ ํ์ฐธ๋์ง๋ง ์์ง ๋ธ๋ผ์ฐ์ (ํนํ ie)๋ค์ด ์ด๋ฅผ ์๋ฒฝํ ์ง์ํ์ง ์๊ณ ์๋ค. ๋ฐ๋ผ์ ํ์ฌ๋ ์ฌ์ ํ es5์์ es6+๋ก ๋์ด๊ฐ๋ ๊ณผ๋๊ธฐ์ธ๋ฐ, ๋คํํ babel์ ๋์์ผ๋ก ์ง๊ธ ๋น์ฅ es6
kim-solshar.tistory.com
์๋ฐ์คํฌ๋ฆฝํธ (arguments ๊ฐ์ฒด, arrow function ํ์ดํํจ์)
1. arguments
medium.com
[JavaScript] Arrow function๊ณผ this ํค์๋
์๋ก ES6์๋ ๋ค์ํ ๋ด์ฉ๋ค์ด ์๋ก ํฌํจ๋์๊ณ , ์ ์ฉํ ๊ฒ๋ค์ด ๋ง๋ค. ๊ทธ๋ ์ง๋ง ์ฌ์ฉ ๋น๋๋ก ์์๋ฅผ ๋งค๊ธด๋ค๋ฉด, ํ์๋ ์ค๋ ๋ค๋ฃฐ Arrow function์ด ๋ค์ฏ ์๊ฐ๋ฝ ์์ ๊ผฝํ ๊ฒ์ด๋ผ๊ณ ํ์ ํ๋ค. ๋ํ, Ar
bluayer.com
https://helloinyong.tistory.com/74
[2019.04.17] ES6 ๋ฌธ๋ฒ - arrow Function๊ณผ ์ผ๋ฐ function์ ์ฐจ์ด
S6์์ arrowFunction ๋ฐฉ์์ผ๋ก ํจ์๋ฅผ ์ ์ธํ ์ ์๋ค๊ณ , ๊ทธ๋ฅ ์ฐ๊ฒ ๋๋ฉด ๋ ํต ๋นํ๋ค... ๋๊ฐ์ด ํจ์๋ฅผ ์ ์ธํ๋ ๋ฐฉ์์ด๊ธด ํ์ง๋ง ๋์ํ๋ ๊ธฐ๋ฅ์ด ๋ค๋ฅธ ํ๊ฐ์ง๊ฐ ์๋ค. ๊ทธ๋ ๊ฒ ๊ณต๋ถํด๋ ์ดํดํ๊ธฐ
helloinyong.tistory.com
https://velog.io/@modolee/javascript-function-vs-arrow-function
JavaScript - Function vs Arrow Function
ES6 ๋ถํฐ ์ถ๊ฐ ๋ Arrow Function์ ๊ธฐ์กด์ ์ผ๋ฐ Function์ ๋น๊ต ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค. ํน์ง. ์ฌ์ฉํ๋ฉด ์๋๋ ๊ฒฝ์ฐ. ํธ์ด์คํ
velog.io
'๐๊ณต๋ถ > JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] var๊ณผ ํธ์ด์คํ (0) | 2021.10.11 |
---|---|
[JavaScript] Callback vs Promise vs async-await (0) | 2021.10.10 |
[JavaScript] window.requestAnimationFrame() (0) | 2021.10.07 |
[JavaScript] Babel (0) | 2021.09.25 |
[JavaScript] ์ด๋ฒคํธ ์์(Event delegation) (0) | 2021.09.23 |
๋๊ธ