๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

javascript23

[JavaScript] var, let, const ์ฐจ์ด โญ var 1. variable(๋ณ€์ˆ˜) 2. ์žฌ์„ ์–ธ ๊ฐ€๋Šฅ ex) var a = 1; console.log(a); // 1 var a = 10; console.log(a); // 10 3. ์žฌ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ ์„ ์–ธํ•œ ๋ณ€์ˆ˜ ์ด๋ฆ„์— ๋‹ค๋ฅธ ๊ฐ’์„ ๋„ฃ์–ด ์„ ์–ธํ•˜๋ฉด ๋‹ค๋ฅธ ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค. ex) var name = 'cat'; console.log(name); // cat var name = 'dog'; console.log(name); // dog 4. ์žฌํ• ๋‹น ๊ฐ€๋Šฅ ex) var a = 1; console.log(a); // 1 a = 10; console.log(a); // 10 5. ํ˜ธ์ด์ŠคํŒ…(Hoisting) ๊ฐ€๋Šฅ var์€ ์„ ์–ธ ๋‹จ๊ณ„์™€ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„๊ฐ€ ํ•œ๋ฒˆ์— ์ด๋ฃจ์–ด์ง„๋‹ค. ๐Ÿ”ฅ ํ˜ธ์ด์ŠคํŒ…(Hoisting)์ด๋ž€?.. 2021. 11. 20.
[JavaScript] ๋‹ค์–‘ํ•œ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ ๐Ÿญ push ๋ฐฐ์—ด ๋’ท๋ถ€๋ถ„์— ๊ฐ’์„ ์‚ฝ์ž…ํ•œ๋‹ค. var arr = ["a", "b", "c", "d"]; arr.push("e"); console.log(arr); // ["a", "b", "c", "d", "e"] ๐Ÿญ pop ๋ฐฐ์—ด ๋’ท๋ถ€๋ถ„์˜ ๊ฐ’์„ ์‚ญ์ œํ•œ๋‹ค. var arr = ["a", "b", "c", "d"]; arr.pop(); console.log(arr); // ["a", "b", "c"] ๐Ÿญ shift ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์˜ ๊ฐ’์„ ์‚ญ์ œํ•œ๋‹ค. var arr = ["a", "b", "c", "d"]; arr.shift(); console.log(arr); // ["b", "c", "d"] ๐Ÿญ unshift ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์— ๊ฐ’์„ ์‚ฝ์ž…ํ•œ๋‹ค. var arr = ["a", "b", "c", "d"]; arr.unshift.. 2021. 11. 18.
์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด(Array-Like Object) ๐Ÿšœ ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด(=์—ฐ๊ด€ ๋ฐฐ์—ด) ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•œ ๊ฐ์ฒด. length ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด(Array-Like Object)๋ผ๊ณ  ํ•œ๋‹ค. DOM์—์„œ ๊ฐ€์ ธ์˜จ element๋“ค ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋กœ ๋“ค ์ˆ˜ ์žˆ๋‹ค. let Object = { 0: 'a', 1: 'b', 2: 'c', length: 3 } ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด๋Š” ์œ„์™€ ๊ฐ™์ด length ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ฃผ๋œ ์šฉ๋„๋Š” key๋กœ ๊ฐ’์„ ๋ฝ‘์•„๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. JavaScript์—์„œ๋Š” ๊ฐ์ฒด๋ฅผ ์—ฐ๊ด€ ๋ฐฐ์—ด๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ •์„์ด๋ผ๊ณ  ํ•œ๋‹ค. console.log ์ฐ์–ด์„œ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋กœ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๐Ÿšœ ํŠน์ง• ๊ฐ์ฒด์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  JavaScript์˜ ํ‘œ์ค€ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.. 2021. 11. 18.
์–•์€ ๋ณต์‚ฌ, ๊นŠ์€ ๋ณต์‚ฌ ๐ŸŽช ์–•์€ ๋ณต์‚ฌ ๋ฐ”๋กœ ์•„๋ž˜ ๋‹จ๊ณ„์˜ ๊ฐ’๋งŒ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ ๐ŸŽช ๊นŠ์€ ๋ณต์‚ฌ ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๊ฐ’๋“ค์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฐพ์•„์„œ ์ „๋ถ€ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ ๐Ÿ‘‰๐Ÿป ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ• 1. ๊ฐ์ฒด์˜ ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฒ”์šฉ ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ var copyObjectDeep = function(target) { var result = {}; if (typeof target === 'object' && target !== null) { for (var prop in target) { result[prop] = copyObjectDeep(target[prop]); } } else { result = target; } return result; } ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ ๋‹ค์Œ์—๋Š” ์›๋ณธ๊ณผ ์‚ฌ๋ณธ์ด ์„œ๋กœ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜์–ด ์–ด๋Š ์ชฝ.. 2021. 11. 17.
immutable.js, baobab.js ๋ถˆ๋ณ€ ๊ฐ์ฒด์˜ ๊ทœ์น™์„ ๋”ฐ๋ฅด๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์‹œ์Šคํ…œ์ ์œผ๋กœ ์ œ์•ฝ์„ ๊ฑฐ๋Š” ํŽธ์ด ์ œ์ผ ์•ˆ์ „ํ•˜๋‹ค. ๋ชจ๋‘๊ฐ€ ๊ทธ ๊ทœ์น™์„ ๋”ฐ๋ฅด์ง€ ์•Š๊ณ ๋Š” ํ”„๋กœํผํ‹ฐ ๋ณ€๊ฒฝ์„ ํ•  ์ˆ˜ ์—†๊ฒŒ๋” ์‹œ์Šคํ…œ์ ์œผ๋กœ ์ œ์•ฝ์„ ๊ฑฐ๋Š” ๊ฒƒ์ด๋‹ค. immutable.js, baobab.js ๋“ฑ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์œ ์šฉํ•˜๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž์ฒด์—์„œ ๋ถˆ๋ณ€์„ฑ์„ ์ง€๋‹Œ ๋ณ„๋„์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ๊ทธ์— ๋”ฐ๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. 2021. 11. 17.
arrow function์—์„œ { }(์ค‘๊ด„ํ˜ธ)๊ฐ€ ์žˆ๊ณ  ์—†๊ณ ์˜ ์ฐจ์ด ๐Ÿค (number) => {return number + 10} return์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผํ•จ. ์•„๋‹ˆ๋ฉด undefined๋กœ ์ธ์‹์„ ํ•จ! ๐Ÿ‘‰๐Ÿป ์ค‘๊ด„ํ˜ธ๋ฅผ ์จ์•ผํ•˜๋Š” ๊ฒฝ์šฐ : ์—ฌ๋Ÿฌ ์ค„์„ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์„ ๋•Œ (number) => { // ์ถ”๊ฐ€์ ์ธ ๋กœ์ง return number + 10 } ๐Ÿค (number) => number + 10 ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ๋”ฑ ๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ์—๋Š” return์„ ์•ˆ ์จ์ค˜๋„ ๋จ! 2021. 11. 16.
๋ฐ˜์‘ํ˜•