๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/๐ŸŽž๏ธ JavaScript

JS/Refactoring | ํด๋กœ์ €, ์ฝœ๋ฐฑ

  • -

 

๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด์„œ ํŒŒ์ผ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ formData๋กœ ๋„ฃ๊ณ , ๋‹ค๋ฅธ ํ•จ์ˆ˜ ์‹คํ–‰์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ณ€์ˆ˜๊ฐ’์„ ์ •์˜ํ•˜์—ฌ ํ•จ์ˆ˜ ์‹คํ–‰์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ์ƒ๊ฐ์ด์˜€์œผ๋‚˜, chatFilePattern์˜ ๋ณ€์ˆ˜๊ฐ’์ด ๊ณ ์ •์ ์œผ๋กœ ๋“ค์–ด๊ฐ€๋ฉด์„œ ์ƒ๊ธด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์ˆ˜์ •ํ•˜๊ณ , ์ˆ˜์ •ํ•œ ์ฝ”๋“œ์— ๋Œ€ํ•œ ๋ฆฌ๋ทฐ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.

for (const file of files) { chatFilePattern = firstNum + "" + countFileNumber++ formData.append("files", file); formData.append("chatFileNumber", chatFilePattern); // Base64 ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ const reader = new FileReader(); reader.readAsDataURL(file); reader.onloadend = () => { var base64data = reader.result; stompSendFlag = 2; stompSend(base64data, chatFilePattern); } }

 

for (const file of files) { // 1. for ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ files ๋ฐฐ์—ด์˜ ๊ฐ ํŒŒ์ผ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•œ๋‹ค. // 2. ๊ฐ ๋ฐ˜๋ณต์—์„œ ์ƒˆ๋กœ์šด chatFilePattern ๊ฐ’์„ ์ƒ์„ฑํ•˜๊ณ  ์ด ๊ฐ’์„ ํŒŒ์ผ๊ณผ ํผ ๋ฐ์ดํ„ฐ์— ์ถ”๊ฐ€ํ•œ๋‹ค. let chatFilePattern = firstNum + "" + countFileNumber++; formData.append("files", file); formData.append("chatFileNumber", chatFilePattern); console.log("chatFilePattern22222222 :::: ", chatFilePattern); // 3. FileReader ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ•ด๋‹น ํŒŒ์ผ์„ readAsDataURL() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฝ๋Š”๋‹ค. const reader = new FileReader(); reader.readAsDataURL(file); // 4. reader.onloadend ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํด๋กœ์ €๋กœ ๊ฐ์‹ธ์„œ ์ •์˜ํ•œ๋‹ค. reader.onloadend = ((currentChatFilePattern) => { return () => { // 5. reader.onloadend ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํด๋กœ์ € ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค. var base64data = reader.result; stompSendFlag = 2; stompSend(base64data, currentChatFilePattern); console.log("chatFilePattern333333333 :::: ", currentChatFilePattern); }; })(chatFilePattern); } // for end

 

  1. for ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ files ๋ฐฐ์—ด์˜ ๊ฐ ํŒŒ์ผ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•œ๋‹ค.
  2. ๊ฐ ๋ฐ˜๋ณต์—์„œ ์ƒˆ๋กœ์šด chatFilePattern ๊ฐ’์„ ์ƒ์„ฑํ•˜๊ณ  ์ด ๊ฐ’์„ ํŒŒ์ผ๊ณผ ํผ ๋ฐ์ดํ„ฐ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  3. FileReader ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ•ด๋‹น ํŒŒ์ผ์„ readAsDataURL() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฝ๋Š”๋‹ค.
  4. reader.onloadend ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํด๋กœ์ €๋กœ ๊ฐ์‹ธ์„œ ์ •์˜ํ•œ๋‹ค.
    • ํด๋กœ์ €๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ด๋‹ค. currentChatFilePattern ์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋กœ์ €๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค.
    • currentChatFilePattern ์€ ๋ฐ˜๋ณต์—์„œ์˜ chatFilePattern ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ํด๋กœ์ €์— ๋ณด์กดํ•œ๋‹ค.
    • ํด๋กœ์ € ํ•จ์ˆ˜๋Š” reader.onloadend ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋œ๋‹ค.
  5. reader.onloadend ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํด๋กœ์ € ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
    • reader.result ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์˜ Base64 ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
    • currentChatFilePattern ์€ ํด๋กœ์ €์— ์˜ํ•ด ๋ณด์กด๋œ ๊ฐ’์ด๋ฏ€๋กœ, ํ•ด๋‹น ๊ฐ’์„ stompSend() ํ•จ์ˆ˜์— ์ „๋‹ฌํ•œ๋‹ค. 
    • stompSend() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ์ดํ„ฐ์™€ currentChatFilePattern ๊ฐ’์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  6. ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜์–ด๊ฐ€๊ณ , ์ƒˆ๋กœ์šด chatFilePattern ๊ฐ’์„ ์ƒ์„ฑํ•˜์—ฌ ๋‹ค์Œ ํŒŒ์ผ์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

 

ํด๋กœ์ € ํ•จ์ˆ˜ ์ •๋ฆฌ

 

 

JavaScript | ํด๋กœ์ € ํ•จ์ˆ˜

๐Ÿ“Œ ํด๋กœ์ € ํ•จ์ˆ˜ ํด๋กœ์ €๋Š” ํ•จ์ˆ˜์™€ ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ์–ดํœ˜์  ํ™˜๊ฒฝ์˜ ์กฐํ•ฉ์ด๋‹ค. ํด๋กœ์ €๋ฅผ ์ดํ•ดํ•˜๋ ค๋ฉด JS๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€์ˆ˜์˜ ์œ ํšจ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š”์ง€๋ฅผ ์ดํ•ดํ•ด์•ผํ•œ๋‹ค. ๐Ÿ“Œ ์–ดํœ˜์  ๋ฒ”์œ„ ์ง€์ •(Lexical scoping) f

mvmthyun.tistory.com

 

 

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.