2021 03 11 - adelakim5/fe-w5-searchUI GitHub Wiki
- ํ๋กํ ํ์ ๊ฐ์ฒด
- session-id ๋ฌธ์
class์ ํํ๊ฐ ์๋๋ผ prototype์ผ๋ก ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ์์๊ด๊ณ๋ฅผ ์ง๋ ์ฐ์ต์ ํด๋ณด์๋ค.
๊ทธ ์ ์ ํ๋กํ ํ์ ์ด๋ ๋ฌด์์ธ์ง์ ๋ํด ์๊ณ ๋์ด๊ฐ๋๊ฒ ์ข์๋ฐ, ์์ค์ ํ๋ ์ฑ ์ด๋ ์ธํฐ๋ท์ ๋๋ฆฐ ํ๋กํ ํ์ ์ ์ ์๋ ํ๋ฒ ๋ด์ ์ข์ฒ๋ผ ์ดํดํ๊ธฐ ์ด๋ ค์ด ๋ฌธ์ฅ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์๋ค.
๊ทธ๋ฌ๋ ๋ช๋ฒ์ ์ค์ต์ ํ๋ฉด์ ๋๋ ํ๋กํ ํ์ ์ด๋, ์ฌ์ ์ ์ ์ ๊ทธ๋๋ก ์๋ฐ์คํฌ๋ฆฝํธ์ ์ํ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ์ฌ์ ๋ค.
let animal = {
eats: true
};
function Rabbit(name) {
this.name = name;
}
Rabbit.prototype = animal; // Rabbit์ ํ๋กํ ํ์
(์ํ)์ animal ๊ฐ์ฒด
let rabbit = new Rabbit("White Rabbit"); // rabbit.__proto__ == animal
์ ์ฝ๋์์ Rabbit.prototype = animal;
์ ํตํด rabbit
์ [[Prototype]]
์ animal
๋ก ์ค์ ๋์๋ค.
์ฆ, Rabbit
์ ์ธ์คํด์ค์ธ rabbit
๊ฐ์ฒด์ ํ๋กํ ํ์
์ animal
์ด ๋๋ ๊ฒ์ด๋ค.
์ด๋ ๊ฒ ํ ๊ฐ์ฒด์ "์ํ"์ ์ค์ ํด์ค์ ๋ฐ๋ผ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์์ ๋๋ฆฌ ์ฌ์ฉ๋๋ "์์" ๊ด๊ณ๋ฅผ ๊ตฌํํ ์ ์๊ฒ ๋๋ ๊ฒ์ด๋ค.
๊ทธ๋ฌ๋ class๋ผ๋ ์์ฃผ ์คํ๋ฐ๋ฆฐ ๋ฌธ๋ฒ์ ์ค๋
๋์ด ์๋ ๋๋ extends, super
๋ฑ๊ณผ ๊ฐ์ ๋ฌธ๋ฒ์ ๋์๋๋ prototype์ ๋ฌธ๋ฒ์์ ๋๊ด์ ๋ง์ด ๋ง์ฃผ์น๊ฒ ๋์๋๋ฐ...
function Keyword(searchingInput, rollingKeywordHtml) {
this.currIndex = 0;
// ...
this.searchingInput = searchingInput;
this.rollingKeywordHtml = rollingKeywordHtml;
}
Keyword.prototype = {
constructor: Keyword,
getTopten,
createTemplate,
init,
};
function getTopten () {
// ...
}
function createTemplate() {
// ...
}
function init() {
// ...
}
Keyword.prototype.fn = function () = {}
๋์ , Keyword.prototype = { fn }
์ ํด๋ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค.
๋จ ์ด๋๋ prototype ๊ฐ์ ๋ค๋ฅธ ๊ฐ์ฒด๋ก ๋ฐ๊พธ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์, contructor
๊ฐ ์กด์ฌํ์ง ์๊ฒ ๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
์ด๋ฅผ ์ํด ์์ constructor = Keyword
๋ฅผ ํด์ค ์ ์๋ค.
์ ๊ฐ์ฒด๋ฅผ ์์๋ฐ๋ ๋ค๋ฅธ ๊ฐ์ฒด๋ค์ ๊ทธ๋ผ ์ด๋ป๊ฒ ์ฌ์ฉํ ์ ์์๊น?
function RecommItems(keywordState, rollings) {
const {searchingInput} = keywordState;
const {rollingKeywordHtml} = rollings;
Keyword.call(this, searchingInput, rollingKeywordHtml); // super()์ ๊ฐ์ ๊ฐ๋
// ...
}
RecommItems.prototype = Object.create(Keyword.prototype); // extends์ ๊ฐ์ ๊ฐ๋
RecommItems.prototype.init = function () {
// ...
// ์์๋ฐ์ initํจ์ ์์ RecommItem์ด ์ํํ ๊ธฐ๋ฅ ๊ตฌํ
}
Keyword.call(this, arg1, arg2)
๋ฅผ ํจ์ผ๋ก์จ Keyword์ ํ๋กํผํฐ๋ค์ RecommItem์ this๋ก ๋งคํํ์ฌ ๊ฐ์ ธ์จ ํ ์ฆ์ ํธ์ถํ๋ฉด **๋ง์น class์์ super()**์ ํ๊ฒ๊ณผ ๊ฐ์ด ๋ถ๋ชจ์ ํ๋กํผํฐ๋ฅผ ์์์๊ฒ๋ก ๊ฐ์ ธ์ค๋ ๊ฒ๊ณผ ๊ฐ๊ฒ ๋๋ค.
๋ํ Object.create(Keyword.prototype)
๋ก Keyword์ ํ๋กํ ํ์
์ ๊ฐ์ง๋ ์ ๊ฐ์ฒด๋ฅผ RecommItems์ ํ๋กํ ํ์
์ผ๋ก ์ง์ ํด์ค์ผ๋ก์จ class์์์ extends ์ฒ๋ผ ์์ ๊ด๊ณ๋ฅผ ์ฐ๊ฒฐ์์ผ์ค๋ค.
RecommItems์ prototype์ Keyword์ prototype์ด ๋๊ณ ๋ ํ, RecommItems์์๋ง ์ฌ์ฉํ๋ ํจ์๋ค์ ๊ตฌํํ๊ณ ์ถ๋ค๋ฉด?
- ์๊น์ฒ๋ผ
RecommItems.prototype = {}
์ผ๋ก ํ๋ฉด ํ๋กํ ํ์ ์ ๋ฎ์ด์์๋ฒ๋ฆฌ๋ ๊ผด์ด ๋์ด๋ฒ๋ฆฐ๋ค. - ๊ธฐ์กด์ prototype ์ํ์ ํจ์๋ฅผ ์ถ๊ฐํ๊ณ ์ถ์ ๊ฒ์ด๋ฏ๋ก
RecommItems.prototype.fn = function () {}
์ด๋ฐ์์ผ๋ก ๊ตฌํํ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ฅผ ๋๋ฒ๊น
ํด๋ก ์ดํด๋ณด๋ฉด __proto__
๊ฐ ๋์จ๋ค. ์ฌ๊ธฐ์ ์ด ๊ฐ์ฒด๊ฐ ์์๋ฐ๊ณ ์๋ ๊ฐ์ฒด๋ ๋ฌด์์ธ์ง์ ๋ํด ์ ์ ์๋ค. (๋น์ฐํ ํด๋น ๊ฐ์ฒด์ ํ๋กํผํฐ๋ ๋์จ๋ค.)
๋๋ ๊ฐ์ธ์ ์ผ๋ก ์ด ์ผ๋ จ์ ๊ณผ์ ์์ ์ ์ด๋ฆ์ด prototype
์ธ์ง์ ๋ํด ๊ฐ์ด ์กํ ๊ฒ ๊ฐ์๋ค.
์ด ๊ฐ์ฒด์ ์ํ์ ๋ญ์ง? -> A ๊ฐ์ฒด -> ์ด ๊ฐ์ฒด์ ์ํ์ ๋ญ์ง? -> B ๊ฐ์ฒด -> ... -> ์ด ๊ฐ์ฒด์ ์ํ์ ๋ญ์ง? -> Object
์ด๋ฒ ๋ฏธ์ ์์ ๋๋ ์ฐ๊ด๊ฒ์์ด๋ฅผ amazon ๊ฒ์ url๋ก ๋ฐ์์๋ค.
๊ทธ๋ฐ๋ฐ, ์ฌ์ฉ์๊ฐ ๊ฒ์์ฐฝ์ ์ ๋ ฅํ ๊ฐ๊ณผ ๋งค์น๋๋ ์ฐ๊ด๊ฒ์์ด์ ์์๋ฅผ ํ์ด๋ผ์ดํธ ํด์ฃผ๋ ๋ถ๋ถ์์ ์ ์ ์๋ ์ค๋ฅ๋ฅผ ๋ง์ฃผ์น๊ฒ ๋์๋ค.
๊ฒ์์ฐฝ์ ์ ๋ ฅ๋ ๊ฐ์ด ์์ด๋ฉด ์๋ฌด ๋ฌธ์ ๊ฐ ์๋๋ฐ, ํ๊ธ์ด๋ฉด ๊ทธ ๊ฐ์ด ์ ๋๋ก ์กํ์ง ์๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
const urls = {
recommendedWords: (inputValue) => `https://completion.amazon.com/api/2017/suggestions?session-id=143-7282527-1203953&customer-id=&request-id=8RSH7H2971TF4M9DSSK8&page-type=Gateway&lop=en_US&site-variant=desktop&client-info=amazon-search-ui&mid=ATVPDKIKX0DER&alias=aps&b2b=0&fresh=0&ks=69&prefix=${inputValue}&event=onKeyPress&limit=11&fb=1&suggestion-type=KEYWORD&suggestion-type=WIDGET&_=1615167756813%27`,
}
async function request(urls, inputValue) {
const response = await fetch(urls(inputValue));
const data = await response.json();
return data;
}
RecommItems.prototype.loadRelatedWords = async function (inputValue) {
const data = await request(urls.recommendedWords, inputValue); // ๋ฐ์ดํฐ ๋ฐ์ ๋๊น์ง ๊ธฐ๋ค๋ฆผ
const { suggestions } = data; // ์ฐ๊ด๊ฒ์์ด ๋ฐฐ์ด
const tempSuggestions = suggestions.map((item) => item.value); // ๊ฐ์ด ์ฐ๊ด๊ฒ์์ด์ธ ์ ๋ค๋ง ๋ฝ์๋
const tempRecommendations = // ํ
ํ๋ ์ดํ
`<div class="recommended">` +
set.reduce((acc, item, i) => {
const highlightOnItem = item.replace(inputValue, `<span class="highlight">${inputValue}</span>`); // inputValue์ ๊ฐ์ ๋ถ๋ถ์ spanํ๊ทธ ๋จน์ธ ๋ฌธ์์ด๋ก ๋์ฒด
acc += `<span class="recommended__item" data-id="${i}">${highlightOnItem}</span>`;
return acc;
}, ``) +
`</div>`;
this.recommWordsToggle.innerHTML = tempRecommendations;
// ...
};
์ด๋ ๊ฒ ํ์ ๋, (๋ง์ฝ ์ฌ์ฉ์๊ฐ "์"๋ฅผ ์ ๋ ฅํ๋ฉด) ๋ด ์์์ผ๋ก๋ ๊ฐ ์ฐ๊ด๊ฒ์์ด๋ค์ด
<span class="recommended__item" data-id="1"><span class="highlight">์</span>๋ง์กด</span>
์ด๋ฐ์์ผ๋ก ๋ฐ๋๋ ๊ฑธ ๊ธฐ๋ํ๋๋ฐ, ์ด์ํ๊ฒ ํ๊ธ์์๋ ๋์๋ค๊ฐ ์๋์๋ค๊ฐ ์ง๋ง๋๋ก(?)์๋ค.
๊ทธ๋ฐ๋ฐ, ๋๋๊ฒ๋ url ์ฃผ์์์ session-id=143-7282527-1203953
๋ถ๋ถ์ ์ง์์คฌ๋๋ ํ๊ธ, ์์ด ๋ชจ๋ ์ ๋ฐ๋์๋ค.
์ด ๋ฌธ์ ์ ๋ํด ๊ณ ๋ฏผํ๋ฉฐ ํด๊ฒฐํ๋ ์ค, ๋ค์๊ณผ ๊ฐ์ด ์์ธ์ ์ถ์ ํ ์ ์์๋ค.
- ํ๊ธ์ ์ ๊ทํ๋ฅผ ํด์ฃผ์ด์ผ ํ๋ค.
data๊ฐ ๋ค์ด์ค๋ ๋ถ๋ถ์์ ๋๋ฒ๊น
์ ํด๋ณด๋, suggestions[0].value[0]
์ ํ์ ๋ "์" ๊ฐ ์๋๋ผ "ใ
"๊ฐ ์ถ๋ ฅ๋์๋ค.
์ฆ, ํ๊ธ์ ์์๊ณผ ๋ชจ์์ด ์กฐํฉ๋ ๋ฌธ์๊ฐ ํ ์์ ์ด๋ผ ๊ฐ๋ณ ์ํ๋ฒณ์ ๋์ด๋ก ์ด๋ฃจ์ด์ง ์์ด์ ๋ฌ๋ผ replace
๊ฐ ๋จนํ์ง ์์๋ ๊ฒ์ด๋ค. ("์" !== "ใ
")
์ด๋ฅผ ์ํด ๊ฐ๊ฐ์ ๋ฐ์ดํฐ ๊ฐ์ String.prototype.normalize()๋ก ๋ชจ๋ ์ ๊ทํ ์์ผ์ฃผ์๋ค.
RecommItems.prototype.loadRelatedWords = async function (inputValue) {
const data = await request(urls.recommendedWords, inputValue);
const { suggestions } = data;
const tempSuggestions = suggestions.map((item) => item.value.normalize("NFC")); // ์ ๊ทํ ์ ์ค ๊ฒฐํฉ (NFC), ์์๋ถ๋ฆฌ ๋์๋ ํ๊ธ์ ๊ฒฐํฉํจ
const tempRecommendations = // ํ
ํ๋ ์ดํ
// ...
}
์ด๋ ๊ฒ ํ๋๋ ํ์ด๋ผ์ดํ ์ด ์ํ๋ ๋๋ก ์ ๋จน์๋ค!
์ด๋ฌํ ๊ณผ์ ์ ํตํด ๋ด๊ฐ ๋๋ ์ ์ ์ ์ธ ๊ฒฐ๋ก ์, ์๋ง์กด url์ ์๋ session-id=143-7282527-1203953
๋ก ์ธํด ๊ธฐ๋ณธ ์ค์ ๊ฐ์ด "NFD"๋ก ๋์ด์์๊ณ , ๊ณต๊ต๋กญ๊ฒ๋ ์ด๊ฑธ ์ ๊ฑฐํจ์ผ๋ก์จ ์์๋ถ๋ฆฌ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ง ์์๋ ๊ฒ์ด ์๋๊น ์ถ๋ค.