์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๐Ÿ“ž : ์—ฌ๊ธฐ์—์„œ ๋„์›€์„ ๋ฐ›์œผ์„ธ์š”

2474 ๋‹จ์–ด codenewbiejavascriptbeginners
์ดˆ๋ณด ๊ฐœ๋ฐœ์ž๋กœ์„œ ์ดํ•ดํ•ด์•ผ ํ•  ๊ฐ€์žฅ ํ˜ผ๋ž€์Šค๋Ÿฌ์šด ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜๋Š” ์ฝœ๋ฐฑ ๊ธฐ๋Šฅ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

์‹ฌ๊ฐํ•˜๊ฒŒ ํ˜ผ๋ž€ ์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ทธ๊ฒƒ๋“ค์„ ์ผ๋ฐ˜ ์˜์–ด๋กœ ๋ถ„ํ•ดํ•ด ๋ด…์‹œ๋‹ค.


์ •์˜



G.O.A.T.MDN์— ๋”ฐ๋ฅด๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

"A function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action."



๋ญ?

์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์‹ค์ œ ์‚ฌ๋ก€



์‹คํ—˜์‹ค์ด ๋ณต์žกํ•ด์ง€๋ฉด์„œ ์ €๋„ ๋ชจ๋ฅด๊ฒŒ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์†”์งํžˆ ์ €์—๊ฒŒ๋Š” ์ด๊ฒƒ์ด ๊ทธ๊ฒƒ๋“ค์„ ๋ฐฐ์šฐ๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์‹คํ—˜์‹ค์„ ์‚ดํŽด๋ณด๊ณ  ์—ฌ๊ธฐ์—์„œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๊ณ  ์žˆ๋Š”์ง€ ๋ด…์‹œ๋‹ค.

์ฒซ์งธ, ๋‚ด ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ๋‚ด JSON ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด .then์—์„œ ์•ฝ์†์œผ๋กœ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜์ธ firstMovie๋Š” ๋‚ด ์›น์‚ฌ์ดํŠธ์— ๊ฐ์ฒด๋กœ ๋„ฃ์–ด์•ผ ํ•˜๋Š” ์˜ํ™”์˜ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด(movieData)์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋“  ๋ณ€์ˆ˜๊ฐ€ ์ •์˜๋˜๊ณ  ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋ณ„๋„์˜ ๋ฒ„ํŠผ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค(๊ฑฑ์ •ํ•  ํ•„์š” ์—†์Œ).

๋‘ ๋ฒˆ์งธ ํ•จ์ˆ˜์ธ createImage๋Š” ๊ฐ ์˜ํ™”์™€ ๊ด€๋ จ๋œ ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•œ ๋‹ค์Œ ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋ฐ”๋กœ ํ‘œ์‹œํ•˜๋„๋ก movieData์— ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ด, firstMovie๊ฐ€ ๋˜ ๋‚˜์™”์–ด! ๊ทธ๊ฒŒ ์–ด๋•Œ์„œ?

createImage๋Š” firstMovie์˜ ๋„์›€ ์—†์ด ์ž์ฒด์ ์œผ๋กœ ๋„ˆ๋ฌด ํฅ๋ฏธ๋กœ์šด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹จ์ง€ ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•  ๋ฟ์ž…๋‹ˆ๋‹ค. ์ด์ „ ๊ธฐ๋Šฅ์ธ firstMovie์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๋งŽ์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

createImage ๋‚ด๋ถ€์—์„œ firstMovie๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ์šฐ๋ฆฌ๋Š” createImage๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ์ด ์ƒˆ ์ด๋ฏธ์ง€์— ๋ชจ๋“  ์˜ํ™”์˜ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐ firstMovie์˜ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋„๋ก ์•Œ๋ฆฌ๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ฆญ ํ•œ ๋ฒˆ์œผ๋กœ firstMovie๊ฐ€ ํ•˜๋Š” ์ผ์„ ์ •ํ™•ํžˆ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. ์˜ํ™”์— ๋Œ€ํ•œ ๋ชจ๋“  JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ  ๊ด€๋ จ ํƒ์ƒ‰ ์ด๋ฏธ์ง€์™€ ํ•จ๊ป˜ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค!

fetch("http://localhost:3000/movies") 
.then(res => res.json()) 
.then(movieData => {
    firstMovie(movieData[0]);
    createImage(movieData);
})

function firstMovie(movieData) {
    title.textContent = movieData.title
    description.textContent = movieData.description
    year.textContent = movieData.release_year
    pic.src = movieData.image
    watchedBtn.addEventListener('click', (e) => {
        movieData.watched = !movieData.watched;
        watchedBtn.textContent = movieData.watched ? "Watched" : "Unwatched";
    });
    }

function createImage(movieData){
    movieData.forEach((movie) => {
        let img = document.createElement("img");
        img.src = movie.image
        insertImage.append(img)
        img.addEventListener('click', (e) => {
            firstMovie(movie)
    });
})
}



๋‹น์‹ ์€ ์ด๊ฒƒ์„ ์–ป์—ˆ๋‹ค.



์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์•„์ง๋„ ํ—ท๊ฐˆ๋ฆฌ์‹ญ๋‹ˆ๊นŒ? ๊ฐ€์žฅ ์œ ์พŒํ•˜๊ฒŒ ์˜์ธํ™”๋œ ๊ธฐ๋Šฅ์„ ๊ฐ–์ถ˜ ์˜ ์ด ๋†€๋ผ์šด YouTube ๋น„๋””์˜ค๋ฅผ ์ฆ๊ธฐ์‹ญ์‹œ์˜ค. ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ์ดํ•ดํ•˜๊ณ  ๋นต์„ ๋จน๊ณ  ์‹ถ์–ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค.



*์ด๋ฏธ์ง€ ์ถœ์ฒ˜: Made by ME!
*

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ