๐
Js_mission - ๋ค์ค ์ปดํฌ๋ํธ
May 16, 2023
ํ์ ๊ตฌํ ์ฌํญ
html
-
<div id="todo-list"></div>์ธ์ ๋ค๋ฅธ div๋ฅผ ๋ ๊ฐ ๋ html ์ฝ๋์ ๋ง๋ญ๋๋ค. -
div์id๋ ์ ๋นํ ์ด๋ฆ์ผ๋ก ์ง์ด์ฃผ์ธ์.
js
-
const data = [{ ... }]์ธ์ todo๋ฅผ ๋ด๊ณ ์๋ array data๋ฅผ ๋ ๊ฐ ๋ ๋ง๋ญ๋๋ค. -
todo์ ๋ด์ฉ์ ๋ณธ์ธ์ ํ์ฌ todo๋ฅผ ๋ด์์ ๋ฃ์ผ๋ฉด ๋ ์ข๊ฒ ์ฃ ?
-
TodoList์ปดํฌ๋ํธ๋ฅผ ์ด ์ธ ๊ฐ ๋ง๋ญ๋๋ค. ์ฒซ ๋ฒ์งธ ์ปดํฌ๋ํธ์๋ ์ ๊ฐ ๋ฃ์ด๋ data์ #todo-list์ ๋ ๋๋งํ๊ณ ,
๋๋จธ์ง ๋ ๊ฐ๋ ์ฌ๋ฌ๋ถ์ด ์ถ๊ฐํ์ div + data๋ฅผ ํ์ฉํด์ ๋ง๋ค์ด์ฃผ์ธ์.
์์ค์ฝ๋
html
<div id="todo-list"></div>
<div id="todo-worship"></div>
<div id="todo-workout"></div>js
const todo_study = [
{
text: 'HTML',
},
{
text: 'Css',
},
{
text: 'Javascript',
},
];
const todo_worship = [
{
text: '์ฝํฐ ์ ํ๊ธฐ',
},
{
text: '์ฝํฐ key ์์ ํ๊ธฐ',
},
];
const todo_workout = [
{
text: '์จ์ดํธ ์ด๋ํ๊ธฐ',
},
{
text: '์ ์ฐ์ ์ด๋ํ๊ธฐ',
},
];
function TodoList(data, id) {
/* ์ธ์คํด์ค ํ์ธ */
if (!new.target) {
throw new Error('is not instance');
}
if (!data) {
throw new Error('check Null or Undefined');
}
this.render = function () {
data.forEach((item) => {
document.querySelector(
`${id}`,
).innerHTML += `<div> ์ค๋์ ํ ์ผ์ ${item.text} ์
๋๋ค!!</div>`;
});
};
}
const study = new TodoList(todo_study, '#todo-list');
const worship = new TodoList(todo_worship, '#todo-worship');
const workout = new TodoList(todo_workout, '#todo-workout');
study.render();
worship.render();
workout.render();TodoList์ id ๋งค๊ฐ๋ณ์ ์ถ๊ฐ
- ํจ์์ id ๋งค๊ฐ๋ณ์๋ฅผ ์ถ๊ฐํด์ document.querySelector๋ฅผ ํตํด ํด๋น id ๊ฐ์ ๊ฐ์ง divํ๊ทธ์ ์ ๊ทผํ๋ค.
- ๊ฐ ์ปดํฌ๋ํธ์ ํด๋น๋๋ data๊ฐ ์ถ๋ ฅ์ด ๋๋๊ฑธ ๋ณผ ์ ์๋ค
๊ฒฐ๊ณผ