Decorators - ChoDragon9/posts GitHub Wiki
๋ฐ์ฝ๋ ์ดํฐ ๊ฐ์
- tc39/proposal-decorators์ ์ ์
- Orthogonal Classes์ Class Evaluation Order ์ ์์ ๋ฐํ์ผ๋ก Decorators์ Class Field ๋ฐ Private methods๋ฅผ ํจ๊ป ์๋์ํค๋ ๋ฐฉ๋ฒ์ ๋ํ ๊ฒฐํฉ ๋ ๋น์ ์ ์ ์
- ์ฅ์์๋ ์ถ๊ฐ ๊ธฐ๋ฅ์ ๋ง๋๋ ๊ฒ์ ์ ์ฉํจ
- ๋ฉ๋ชจ์ด์ ์ด์
, ์ ๊ทผ ์ ์ด, ์ธ์ฆ, ๊ณ์ธก, ํ์ด๋ฐ ์ฒ๋ฆฌ, ๋ก๊น
, ์๋ ์ ํ ๋ฑ์ ์ฌ์ฉ๋๋ค.
- ์๋์ ๊ฐ์ ๊ธฐ๋ฅ์ ํ ์ ์๋ค.
@defineElement
์ปค์คํ
์๋ ๋ฉํธ๋ฅผ ์์ฑํ๋ ๊ธฐ๋ฅ
@bound
๋๋ฐ์ด๋ ์ฒ๋ฆฌ ๊ธฐ๋ฅ
@observed
ํ๋๋ฅผ ๊ฐ์ํ๋ฉฐ ๋ณ๊ฒฝ์ ์๋์ผ๋ก render()
๋ฅผ ํธ์ถํ๋ ๊ธฐ๋ฅ
@defineElement('num-counter')
class Counter extends HTMLElement {
@observed #x = 0;
@bound
#clicked() {
this.#x++;
}
constructor() {
super();
this.onclick = this.#clicked;
}
connectedCallback() { this.render(); }
@bound
render() {
this.textContent = this.#x.toString();
}
}
Orthogonal = ์ง๊ต์ฑ
- ์ง๊ต๋ ๋ ์ด์์ ์๋ก ๋ค๋ฅธ ์ฒด๊ณ๊ฐ ์๋ก ์ํฅ์ ์ฃผ์ง ์์ผ๋ฉด์ ํจ๊ป ๋์ํ ์ ์๋ ์ํ๋ ํน์ง์ ๋งํ๋ค.
- ์ด๋ค ํ๋ก๊ทธ๋จ์ด ๋ณ๊ฒฝ์ด ๋๋ ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ ์ํฅ์ ์ฃผ์ง ์๋ ๋ค๋ฉด ์๋ก ์ง๊ตํ๋ค๊ณ ํ๋ค.
- ์ผ์ข
์ ๋
๋ฆฝ์ฑ, ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๋ ๊ฒ์ ๋งํ๋ค.
Field declarations
// ES2015
class Counter {
constructor () {
this.x = 0
}
}
// ESnext field declarations proposal
class Counter {
x = 0
constructor () { }
}
Private methods and fields
class Counter {
#x = 0
#clicked () { }
}