์ต๊ทผ์ ์ ๊ท ๊ธฐ๋ฅ ๊ฐ๋ฐ์ ์งํํ๋ฉด์, EyeDropper API๋ฅผ ์ฌ์ฉ์ด ํ์ํ๋ค. ์ฌ์ฉ์๊ฐ ํ๋ฉด์ ํ์๋ ์์์ ์ ํํ๊ณ ๊ทธ ์ ๋ณด๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๊ฐ์ ธ์ฌ ์ ์๋ Web API ์ด์๋๋ฐ, ์์ง ๋๋ถ๋ถ์ ๋ธ๋ผ์ฐ์ ์์ ํด๋น API๋ฅผ ๊ณต์ ์ง์ํ์ง ์๊ฑฐ๋, ์ง์ ๋ฒ์๊ฐ ์ ํ์ ์ด๋ผ ํ๋ก์ ํธ์ ๋ฐ๋ก ์ ์ฉํ๋ ๋ฐ์๋ ๋ฌด๋ฆฌ๊ฐ ์์๋ค. ๐ฅฒ๐ฆ
์ด๋ ํ์๋ถ์ด EyeDropper Polyfill ์ด ์๋ค๋ ๊ฒ์ ์๋ ค์ฃผ์ จ๋๋ฐ, Polyfill์ด ๋ฌด์์ธ์ง, ๊ทธ๋ฆฌ๊ณ EyeDropper Polyfill์ ์ด๋ป๊ฒ ๊ตฌํ๋์ด ์๋์ง ์ ๋ฆฌํ๊ณ ์ ํ๋ค.
Polyfill์ด๋?
Polyfill์ ์ฝ๊ฒ ๋งํด ์ต์ ๋ธ๋ผ์ฐ์ ๊ธฐ๋ฅ์ ์ง์ํ์ง ์๋ ๊ตฌํ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํด ๊ทธ ๊ธฐ๋ฅ์ ๋์ ๊ตฌํํด ์ฃผ๋ ‘์ฝ๋’์ด๋ค.
์๋ฅผ ๋ค์ด, ES6์์ ์๋กญ๊ฒ ๋์
๋ ๋ฉ์๋๋ API๋ค์ด IE๋ ์ค๋๋ ๋ธ๋ผ์ฐ์ ์๋ ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์๋ฐ, ์ด๋ ‘์ด๋ฏธ ๊ตฌํ ํ๊ฒฝ์์๋ ๋์ํ๋๋ก’ JS ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ฝ๋๋ก ์ง์ ์ฌํํด ๋ด๋ ๊ฒ์ด Polyfill์ด๋ผ๊ณ ํ๋ค.
์ฆ, ํน์ ๊ธฐ๋ฅ์ ์ํํ์ง ๋ชปํ๋ ๋ธ๋ผ์ฐ์ ์์๋ ๊ทธ ๊ธฐ๋ฅ์ด ๊ฐ๋ฅํ ๊ฒ์ฒ๋ผ ๋์ํ๊ฒ ๋ง๋ค์ด์ค๋ค.
โ ์ ๋ฆฌํ์๋ฉด
- ์ง์ํ์ง ์๋ ๊ธฐ๋ฅ์ ์๋ฎฌ๋ ์ด์ ํ๋ค.
- ์ต์ ์ฌ์์ ์ค๋๋ ๋ธ๋ผ์ฐ์ ์์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ํด ์ค๋ค.
EyeDropper Polyfill ์ฝ๋ ๊ตฌํ ๋ฐฉ์ ์ดํด๋ณด๊ธฐ
eyedropper-polyfill๋ EyeDropper API๋ฅผ ์ ์ ์ง์ํ์ง ์๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ผํ ์ฌ์ฉ์ ๊ฒฝํ์ ์ ๊ณตํ๋๋ก ํด์ฃผ๋ ์ญํ ์ ํ๋ค.
์ฝ๋๋ฅผ ์ดํด๋ณด๋ฉด 1. ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์ EyeDropper API ์ง์ ์ฌ๋ถ๋ฅผ ์ ๊ฒํ ๋ค, ์ง์ํ์ง ์์ ๊ฒฝ์ฐ 2. Polyfill์ ๋์ ์ผ๋ก ์ฃผ์ ํ๋ ๋ก์ง์ด๋ค.
import { isEyeDropperSupported } from './support';
import { attachPolyfill } from './attach';
if (!isEyeDropperSupported()) {
attachPolyfill();
}
1. isEyeDropperSupported() ํจ์ ํธ์ถ
์ด ํจ์๋ ํ์ฌ ์คํ๋๋ ํ๊ฒฝ(๋ธ๋ผ์ฐ์ )์์ EyeDropper API๋ฅผ ์ง์ํ๋์ง ํ๋ณํ๋ค.
export function isEyeDropperSupported(): boolean {
return 'EyeDropper' in window;
}
if (! isEyeDropperSupported()) {... }์
ํตํด ๋ธ๋ผ์ฐ์ ๊ฐ EyeDropper๋ฅผ ์ง์ํ์ง ์๋ ๊ฒฝ์ฐ์๋ง attachPolyfill();
์ด ์คํ๋๋ค.
2. attachPolyfill ํจ์ ํธ์ถ
import { EyeDropperPolyfill } from './eyedropper';
export function attachPolyfill() {
if (!Reflect.defineProperty(window, 'EyeDropper', { value: EyeDropperPolyfill })) {
throw Error("Error attaching `EyeDropper` polyfill: couldn't attach `EyeDropper` to `window`");
}
}
- EyeDropperPolyfill ํด๋์ค๋ฅผ ๋ถ๋ฌ์จ๋ค. ์ด ํด๋์ค๋ ์ค์ ๋ธ๋ผ์ฐ์ ๊ฐ ์ ๊ณตํ๋ EyeDropper API๋ฅผ ํ๋ด ๋ด์ด ๊ตฌํํ ํด๋ฆฌํ์ด๋ค
- Reflect.defineProperty ์ฌ์ฉ
- Reflect.defineProperty๋ Object.defineProperty์ ๊ฑฐ์ ์ ์ฌํ๊ฒ ๋์ํ์ง๋ง, writable, enumerable, configurable ๋ฑ์ ํน์ฑ์ ์กฐ๊ธ ๋ ์ธ๋ฐํ๊ฒ ์กฐ์ ํ ์ ์๋ค๊ณ ํ๋ค.
๊ฐ๋จํ๊ฒ ์ ๋ฆฌ๋ฅผ ํ์๋ฉด
- ์ฐ์ ํ์ฌ ๋ธ๋ผ์ฐ์ ๊ฐ EyeDropper API๋ฅผ ์ง์ํ๋์ง ํ์ธํ๋ค. (์ง์ํ๋ค๋ฉด, ์ถ๊ฐ ์์ ์์ด ๋ค์ดํฐ๋ธ API๋ฅผ ์ฌ์ฉ!)
- ์ง์ํ์ง ์๋๋ค๋ฉด attachPolyfill()์ ํตํด EyeDropper ํด๋์ค๋ฅผ ์ ์ํ๊ณ , ๊ธฐ์กด ์ฝ๋์์ EyeDropper API๋ฅผ ์ฌ์ฉํ๋ ๋ถ๋ถ์ด ๋ฌธ์ ์์ด ๋์ํ๊ฒ ๋ง๋ ๋ค.
๐ค Polyfill์ ๋ํด ์์๊ฐ๋ค ๋ณด๋ฉด “์ง์ํ์ง ์๋ API๋ ๊ทธ๋ฅ ๋ชจ๋ ํด๋ฆฌํ๋ก ํด๊ฒฐํ๋ฉด ๋์ง ์์๊น?”๋ผ๋ ์๊ฐ์ด ๋ค๊ธฐ๊ฐ ์ฌ์ด ๊ฒ ๊ฐ๋ค. ๊ฒฐ๋ก ๋ถํฐ ๋งํ์๋ฉด ๋ฌด๋ถ๋ณํ๊ฒ ํด๋ฆฌํ์ ๋์ ํ๋ ๊ฒ์ ์ง์ํด์ผ ํ๋๋ฐ, ์๋๋ฉด ํด๋ฆฌํ๋ ๊ฒฐ๊ตญ JS ์ฝ๋์ด๊ธฐ ๋๋ฌธ์ ๋ฌด๋ถ๋ณํ๊ฒ ์ฌ์ฉํ๋ฉด ๊ฒฐ๊ตญ ๋ก๋ฉํด์ผ ํ๋ ์คํฌ๋ฆฝํธ ์์ด ๋์ด๋๊ณ , ์คํํด์ผ ํ ์ฝ๋๋ ๋ง์์ง๊ณ ๊ฒฐ๊ตญ ์ฑ์ ์ฑ๋ฅ ์ ํ๋ก ์ด์ด์ง ์ ์๋ค๊ณ ํ๋ค. ๋ฐ๋ผ์ ํด๋ฆฌํ์ ๋ฐ๋์ ํ์ํ ๋ถ๋ถ์๋ง, ๊ฐ๋ฅํ ํ ๊ฐ๋ณ๊ฒ ์ ์ฉํ๋๋ก ํ์.
https://developer.mozilla.org/ko/docs/Glossary/Polyfill
https://toss.tech/article/smart-polyfills
'๐ WIL > ๐ JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
CustomEvent (3) | 2024.11.10 |
---|---|
๐ก axios await/async vs then (0) | 2022.08.11 |
๐ก ํ์๋๋ก e.target.value๋ฅผ ์ด์ฉํ๋๋ฐ, ์์๊ฐ์ ๊ฐ์ ธ์ค์ง ๋ชปํ๋ค..? - ํ ํ๋ก์ ํธ (0) | 2022.08.11 |
โ๏ธ ๋น๊ตฌ์กฐํ ํ ๋น (0) | 2022.08.10 |
๐ก ์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ (0) | 2022.08.10 |