1. ์ ํด๋์ค?
- ๋จ์ ํจ์์ ์ ์ญ ๋ณ์: ๊ฐ์ฅ ๊ฐ๋จํ์ง๋ง, ์ฌ๋ฌ ์นด์ดํฐ๋ฅผ ๋ง๋ค ๊ฒฝ์ฐ ์ํ ๊ด๋ฆฌ๊ฐ ๋ณต์กํด์ง๊ณ ๋ณ์ ์ด๋ฆ ์ถฉ๋์ด๋ ์ค์ฝํ ๋ฌธ์ ๊ฐ ์๊ธฐ๊ธฐ ์ฌ์ ๋ค.
- ํด๋ก์ (Closure) ํ์ฉ: ํด๋ก์ ๋ฅผ ์ด์ฉํด ์ํ๋ฅผ ์จ๊ธฐ๊ณ ํจ์๋ฅผ ๋ฐํํ๋ ํฉํ ๋ฆฌ ํจ์ ํจํด๋ ๊ฐ๋ฅํ๋ค. ์ํ๋ฅผ ๋น๊ณต๊ฐ๋ก ์ ์งํ ์ ์๋ค๋ ์ฅ์ ์ด ์์ง๋ง, ์ฌ๋ฌ ์ธ์คํด์ค๋ฅผ ๋ง๋ค ๋๋ง๋ค ํจ์๋ค์ด ์๋ก ์์ฑ๋๋ ์ , ๊ทธ๋ฆฌ๊ณ 'ํ'๋ก์์ ๋ช ํ์ฑ์ด ํด๋์ค๋ณด๋ค๋ ๋ถ์กฑํ๋ค๊ณ ๋๊ปด์ก๋ค.
- ES5 ์์ฑ์ ํจ์์ ํ๋กํ ํ์ : new ํค์๋์ ํจ๊ป ์ฌ์ฉํ๋ ์ ํต์ ์ธ ๋ฐฉ์์ด๋ค. ํด๋์ค์ ๊ฐ์ฅ ์ ์ฌํ์ง๋ง, prototype์ ์ง์ ๋ค๋ค์ผ ํด์ ๋ฌธ๋ฒ์ด ๋ค์ ๋ฒ๊ฑฐ๋กญ๊ณ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ค๊ณ ์๊ฐํ๋ค.
์ด๋ฐ ์ ๋ค์ ๊ณ ๋ คํ์ ๋, ES6์ class ๋ฌธ๋ฒ์ด ๊ด๋ จ ๋ฐ์ดํฐ(์ํ)์ ๊ธฐ๋ฅ(๋ฉ์๋)์ ํ๋์ '์ค๊ณ๋'์ฒ๋ผ ๋ฌถ์ด์ฃผ๊ณ , new ํค์๋๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๋ฐฉ์์ด ๊ฐ์ฅ ์ง๊ด์ ์ด๊ณ ๊ตฌ์กฐ์ ์ผ๋ก ๋ณด์๋ค. ์ฝ๋์ ์๋๋ฅผ ๋ช ํํ ๋๋ฌ๋ด๊ณ , ๊ฐ์ฒด ์งํฅ์ ๊ฐ๋ ์ ์ ์ฉํ๊ธฐ์๋ ์ ํฉํ๋ค๊ณ ํ๋จํ๋ค.
2. ์นด์ดํฐ ํด๋์ค ๊ตฌ์ํ๊ธฐ (์ค๊ณ ๋จ๊ณ)
ํด๋์ค๋ฅผ ์ฌ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ๊ณ , ๊ตฌ์ฒด์ ์ผ๋ก ์นด์ดํฐ ์ปดํฌ๋ํธ์ ํ์ํ ์์๋ค์ ์ค๊ณํ๊ธฐ ์์ํ๋ค.
- ํต์ฌ ๋ฐ์ดํฐ (Properties):
- count: ํ์ฌ ์ซ์ ๊ฐ์ ์ ์ฅํ ๋ด๋ถ ์ํ. ์ด๊ธฐ๊ฐ์ 0์ผ๋ก ์ค์ ํ๋ค.
- displayElement: ์นด์ดํฐ ๊ฐ์ ํ์ํ ์ธ๋ถ DOM ์์. ํด๋์ค ์ธ๋ถ์์ ๋ฐ์์์ผ ํ๋ค.
- incrementButton: ์นด์ดํฐ๋ฅผ ์ฆ๊ฐ์ํฌ ๋ฒํผ DOM ์์. ์ด๊ฒ๋ ์ธ๋ถ์์ ๋ฐ์์จ๋ค.
- (์ถ๊ฐ ๊ณ ๋ ค) decrementButton: ๊ฐ์ ๊ธฐ๋ฅ๋ ๋ฃ๋๋ค๋ฉด ํ์ํ ์ ์๋ค. (์ด๋ฒ์ ์ฆ๊ฐ๋ง ๊ตฌํํ๊ธฐ๋ก ํจ)
- ํต์ฌ ๊ธฐ๋ฅ (Methods):
- increment(): count ์์ฑ ๊ฐ์ 1 ์ฆ๊ฐ์ํจ๋ค.
- updateDisplay(): ํ์ฌ count ๊ฐ์ displayElement์ ํ ์คํธ ๋ด์ฉ์ผ๋ก ์ ๋ฐ์ดํธํ๋ค. ์ด ๋ฉ์๋๋ increment ๋ด๋ถ์์๋ ํธ์ถ๋์ด์ผ ํ๊ณ , ์ด๊ธฐํ ์์ ์๋ ํธ์ถ๋์ด์ผ ํ๋ค.
- ์ด๊ธฐ ์ค์ (Constructor):
- displayElement์ incrementButton์ ์ธ์๋ก ๋ฐ๋๋ค.
- ๋ฐ์์จ DOM ์์๋ค์ ํด๋์ค ๋ด๋ถ ์์ฑ(e.g., this.displayElement)์ ํ ๋นํ๋ค.
- count ์์ฑ์ 0์ผ๋ก ์ด๊ธฐํํ๋ค.
- ์ด๊ธฐ ํ๋ฉด ํ์๋ฅผ ์ํด this.updateDisplay()๋ฅผ ํธ์ถํ๋ค.
- ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ๊ฐ์งํ๊ณ , ํด๋ฆญ ์ this.increment ๋ฉ์๋๊ฐ ์คํ๋๋๋ก ์ฐ๊ฒฐํ๋ค. (์ด ๋ถ๋ถ์์ this ๋ฌธ์ ๊ฐ ์์๋์๋ค.)
์ฒ์์๋ ๋จ์ํ ๊ธฐ๋ฅ๋ง ๋์ดํ๋๋ฐ, ์ด๋ ๊ฒ ์์ฑ(๋ฐ์ดํฐ)๊ณผ ๋ฉ์๋(๊ธฐ๋ฅ), ๊ทธ๋ฆฌ๊ณ ์์ฑ์(์ด๊ธฐํ)๋ก ๋๋์ด ์๊ฐํ๋ ํด๋์ค์ ๊ตฌ์กฐ๊ฐ ๋ ๋ช ํํด์ก๋ค.
3. ๊ตฌํ ์์: constructor์ ์ฒซ this์ ๋ฒฝ
์ค๊ณ๋ฅผ ๋ฐํ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ์์ํ๋ค. ์์ฑ์์์ ์ธ๋ถ ์์๋ฅผ ๋ฐ์์ ๋ด๋ถ ์์ฑ์ ํ ๋นํ๋ ๊ฒ ์์ฒด๋ ์ด๋ ต์ง ์์๋ค.
class Counter {
constructor(displayEl, buttonEl) {
// ์ด๊ธฐ๊ฐ ์ค์
this.count = 0;
// ์ ๋ฌ๋ฐ์ DOM ์์๋ฅผ ๋ด๋ถ ์์ฑ์ ํ ๋น
// 'this'๋ ์์ฑ๋ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
this.displayElement = displayEl;
this.buttonElement = buttonEl;
console.log('Counter ์ธ์คํด์ค ์์ฑ๋จ:', this); // ๋๋ฒ๊น
: this ํ์ธ
// ์ด๊ธฐ ํ๋ฉด ์
๋ฐ์ดํธ (๋ฉ์๋ ํธ์ถ)
this.updateDisplay();
// ์ฌ๊ธฐ์๋ถํฐ this ๋ฌธ์ ๋ฐ์ ๊ฐ๋ฅ์ฑ ์ธ์ง
this.buttonElement.addEventListener('click', /* ??? */);
}
// ๋ฉ์๋ ์ ์...
increment() {
this.count++;
console.log('Incrementing count to:', this.count); // ๋๋ฒ๊น
this.updateDisplay();
}
updateDisplay() {
console.log('Updating display with count:', this.count); // ๋๋ฒ๊น
this.displayElement.textContent = this.count;
}
}
์ฌ๊ธฐ๊น์ง๋ ๊ด์ฐฎ์๋ค. this๊ฐ ์๋ก ์์ฑ๋ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํจ๋ค๋ ์ ๋ง ์ดํดํ๋ฉด ๋๋ค. ๋ฌธ์ ๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ค์ ํ๋ ๋ถ๋ถ์์ ๋ฐ์ํ๋ค.
4. ์ด๋ฒคํธ ํธ๋ค๋ฌ์ this์ ์ฌ์, ๊ทธ๋ฆฌ๊ณ ํด๊ฒฐ ๊ณผ์
๊ฐ์ฅ ํฐ ๋๊ด์ ์ญ์ this์๋ค. ๋ฒํผ ํด๋ฆญ ์ increment ๋ฉ์๋๋ฅผ ์คํํ๋๋ก ์๋์ฒ๋ผ ์ฐ๊ฒฐํ๋๋ ์์๋๋ก ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
// ๋ฌธ์ ์ ์ฝ๋ (constructor ๋ด๋ถ)
this.buttonElement.addEventListener('click', this.increment);
๋ฒํผ์ ํด๋ฆญํ๋ฉด increment ๋ฉ์๋๊ฐ ์คํ๋๊ธด ํ์ง๋ง, ๊ทธ ์์์ this๊ฐ Counter ์ธ์คํด์ค๊ฐ ์๋๋ผ this.buttonElement (์ฆ, ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํจ ๋ฒํผ ์์)๋ฅผ ๊ฐ๋ฆฌ์ผฐ๋ค. ์๋ํ๋ฉด addEventListener์ ์ฝ๋ฐฑ ํจ์๋ก ๋ฉ์๋๊ฐ ์ ๋ฌ๋ ๋, ๋ฉ์๋ ํธ์ถ์ ์ฃผ์ฒด(context)๊ฐ Counter ์ธ์คํด์ค๊ฐ ์๋๋ผ ๋ฒํผ ์์ ์์ฒด๊ฐ ๋๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋์ increment ๋ด๋ถ์ this.count++๋ this.updateDisplay()๋ ๋ฒํผ ์์์ count ์์ฑ์ด๋ updateDisplay ๋ฉ์๋๋ฅผ ์ฐพ์ผ๋ ค๋ค ์คํจํ๊ณ ์๋ฌ๋ฅผ ๋ฐ์์์ผฐ๋ค (TypeError).
์ด๊ฑธ ํ์ธํ๊ธฐ ์ํด increment ๋ฉ์๋ ๋งจ ์์ console.log('this in increment:', this); ๋ฅผ ๋ฃ์ด๋ณด๋ ๋ช ํํ ๋ฒํผ ์์๊ฐ ์ถ๋ ฅ๋์๋ค.
ํด๊ฒฐ์ฑ 1: Function.prototype.bind()
bind ๋ฉ์๋๋ ํธ์ถ๋ ๋์ this ๊ฐ์ ํน์ ๊ฐ์ฒด๋ก ๊ณ ์ ํ ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ค. ์ด๊ฑธ ์ฌ์ฉํ๋ฉด increment ๋ด๋ถ์ this๊ฐ ํญ์ Counter ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ํ ์ ์๋ค.
// ํด๊ฒฐ ์ฝ๋ 1 (constructor ๋ด๋ถ)
this.buttonElement.addEventListener('click', this.increment.bind(this));
// this.increment ํจ์๋ฅผ ํธ์ถํ๋, ๋ด๋ถ์ 'this'๋ ํญ์ ํ์ฌ Counter ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ๋ฌถ์
ํด๊ฒฐ์ฑ 2: ํ์ดํ ํจ์ (ํด๋์ค ํ๋ ๋ฌธ๋ฒ ํ์ฉ)
๋ฉ์๋๋ฅผ ์ ์ํ ๋ ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด, ํด๋น ํจ์๋ ์์ ๋ง์ this๋ฅผ ๊ฐ์ง ์๊ณ ์์ ์ ๋๋ฌ์ผ ์ธ๋ถ ์ค์ฝํ(์ฌ๊ธฐ์๋ constructor ๋๋ ํด๋์ค ์์ฒด)์ this๋ฅผ ๊ทธ๋๋ก ์์๋ฐ๋๋ค. ์ฆ, Counter ์ธ์คํด์ค๋ฅผ this๋ก ์ฌ์ฉํ๊ฒ ๋๋ค. ์ด๊ฑธ ์ฌ์ฉํ๋ฉด bind ์์ด๋ ๊น๋ํ๊ฒ ํด๊ฒฐ ๊ฐ๋ฅํ๋ค. (๋จ, ํด๋์ค ํ๋ ๋ฌธ๋ฒ์ ๋น๊ต์ ์ต์ ์คํ์ด๋ฏ๋ก ๋ธ๋ผ์ฐ์ ํธํ์ฑ ํ์ธ์ด ํ์ํ ์ ์๋ค.)
class CounterWithArrowFn {
constructor(displayEl, buttonEl) {
this.count = 0;
this.displayElement = displayEl;
this.buttonElement = buttonEl;
this.updateDisplay();
// ํ์ดํ ํจ์๋ก ์ ์๋ ๋ฉ์๋๋ this๊ฐ ์๋์ผ๋ก ๋ฐ์ธ๋ฉ๋จ
this.buttonElement.addEventListener('click', this.increment);
}
// ๋ฉ์๋๋ฅผ ํ์ดํ ํจ์๋ก ์ ์
increment = () => {
this.count++;
// ์ด ์์ this๋ ํญ์ Counter ์ธ์คํด์ค
this.updateDisplay();
}
updateDisplay = () => {
this.displayElement.textContent = this.count;
}
}
๋ ๋ฐฉ๋ฒ ๋ชจ๋ ์ ํจํ์ง๋ง, ํ์ดํ ํจ์ ๋ฐฉ์์ด ์ฝ๋๊ฐ ๋ ๊ฐ๊ฒฐํด ๋ณด์๋ค. this ๋ฌธ์ ํ๋ ํด๊ฒฐํ๋ ๋ฐ๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ์คํ ์ปจํ ์คํธ์ ํจ์์ ๋์ ๋ฐฉ์์ ๋ํ ๊น์ ์ดํด๊ฐ ํ์ํ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์๋ค.
5. ๋ฐฐ์ด ์ ์ ๋ฆฌ
์ด๋ฒ ๋ฐ๋๋ผ JS ํด๋์ค ๊ตฌํ ๊ฒฝํ์ ํตํด ๋ช ๊ฐ์ง ์ค์ํ ์ ์ ๋ฐฐ์ธ ์ ์์๋ค.
- ํด๋์ค๋ ๊ด๋ จ ๋ฐ์ดํฐ์ ๊ธฐ๋ฅ์ ์บก์ํํ์ฌ ์ฝ๋๋ฅผ ๊ตฌ์กฐํํ๋ ๋ฐ ์ ์ฉํ๋ค.
- ํด๋์ค ์ค๊ณ ๋จ๊ณ์์ ํ์ํ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ๋ฏธ๋ฆฌ ์ ์ํ๋ ๊ฒ์ด ๊ตฌํ์ ๋์์ด ๋๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ์์ this๋ ํจ์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ฏ๋ก, ํนํ ์ฝ๋ฐฑ ํจ์๋ก ๋ฉ์๋๋ฅผ ์ ๋ฌํ ๋ ์ฃผ์๊ฐ ํ์ํ๋ค.
- bind๋ ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉ์๋ ๋ด์ this ์ปจํ ์คํธ๋ฅผ ๋ช ์์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ค.
'๐ป FE > ๐ JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฌ๊ท์ ์ (0) | 2025.04.26 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ์์ on๊ณผ handle์ ๊ด๊ณ์ ๋ํด ์์๋ณด์๋ค. (0) | 2025.04.20 |
์ด์ ๋ MVC, ์ค๋์ ๋ฐ๋๋ผJS๋ก SPA (0) | 2025.04.03 |
MVC ํจํด ์ตํ๊ธฐ | ๊ฐ๋จํ(์ ํ ๊ฐ๋จํ์ง ์๋ค.) ํด์ฆ ์ฑ MVC ํจํด์ผ๋ก ๋ง๋ค๊ธฐ (0) | 2025.04.02 |
MDN ๋ฒ์ญ | Window: requestAnimationFrame() method (0) | 2025.03.21 |