κ°μ²΄(Object)λ₯Ό 볡μ¬νλ λ°©λ²μ μμ 볡μ¬(Shallow Copy) μ κΉμ 볡μ¬(Deep Copy) λ‘ λλ©λλ€.
μ¬μ©νλ λ©μλλ μ¬λΏ μμ§λ§, 컨μ μ 2κ°μ§ μ λλ€. κ°μ μ£Όμ κ°μ 볡μ¬ν΄ μ€ κ²μΈμ§, κ° μ체λ₯Ό 볡μ¬ν΄ μ€ κ²μΈμ§μ.
κ°μ²΄κ°μ μ°Έμ‘°ν λ°μ΄ν°λ₯Ό 볡μ¬νκ² λλ©΄, κ°μ΄ λ©λͺ¨λ¦¬ μ μ ν λΉλλ κ²μ΄ μλλΌ κ°μ΄ λ€μ΄ μλ μ£Όμκ°(μ°Έμ‘°κ°) μ΄ λ©λͺ¨λ¦¬ μ μ λ€μ΄ κ°λλ€. λλ¬Έμ 볡μ¬λ₯Ό νκ² λλ©΄, κ° μμ²΄κ° μλλΌ κ°μ΄ λ€μ΄κ° μλ μ£Όμκ° μ λ¬ λ©λλ€. μ΄λ κ² λ³΅μ¬λλ κ²μ Shallow Copy λΌκ³ ν©λλ€.
Shallow Copy
spread syntax, Array.prototype.concat(), Array.prototype.slice(),Array.from(), Object.assign(), Object.create() ....
μλ°μ€ν¬λ¦½νΈκ° μ 곡ν΄μ£Όλ λ©μλλ€μ μ΄μ©ν΄μ Shallow Copy κ° κ°λ₯ν©λλ€.
μμ μ λλ€.
let ingredients_list = ["noodles",{"list":["eggs","flour","water"]}];
let ingredients_list_copy = Array.from(ingredients_list);
ingredients_list_copy[1].list = ["rice flour","water"] // 볡μ¬ν κ°μ²΄μ κ°μ λ³κ²½ ν©λλ€.
console.log(JSON.stringify(ingredients_list)); // μλ³Έλ μμ λμμ΅λλ€.
// ["noodles",{"list":["rice flour","water"]}]
κ°μ²΄μ κ°μ λ³κ²½ν λλ κ°μ μ£Όμλ₯Ό 곡μ νκ³ μλ λ€λ₯Έ κ°μ²΄λ€λ κ³ λ €ν΄μΌ ν©λλ€.
Deep Copy
Object κ° serialized νλ€λ©΄, JSON.stringify() λ‘ JSON string μΌλ‘ λ§λ νμ JSON.parse() λ₯Ό μ΄μ©ν΄μ μλ‘μ΄ Object λ‘ λ§λ€ μ μμ΅λλ€. μ¦, μ£Όμκ° μλ κ° μ체λ₯Ό 볡μ¬ν΄μ λ£μ΄ μ€λ€λ λ»μ λλ€.
μ¬κΈ°μ serialized νλ€λ κ²μ μ»΄ν¨ν° λ©λͺ¨λ¦¬ μμ μ‘΄μ¬νλ κ°μ²΄(Object)λ₯Ό λ¬Έμμ΄(string) λ‘ λ³ν κ°λ₯ν κ²μ λ§ν©λλ€.
let ingredients_list = ["noodles",{"list":["eggs","flour","water"]}];
let ingredients_list_deepcopy = JSON.parse(JSON.stringify(ingredients_list));
// deepcopy λ κ°μ²΄μ κ°μ μμ ν©λλ€.
ingredients_list_deepcopy[1].list = ["rice flour","water"]
// μλ³Έ κ°μ²΄λ μμ λμ§ μμ κ²μ μ μ μμ΅λλ€.
console.log(ingredients_list[1].list);
// Array(3) [ "eggs", "flour", "water" ]
κ·Έλ¬λ λλΆλΆμ Object λ μμ κ°μ΄ λ¨μν κ΅¬μ‘°λ‘ κ΅¬μ±λμ΄ μμ§ μμ΅λλ€.
μλ₯Ό λ€μ΄, function, Symbols, DOM API μ HTML μμλ€ λ±μ JSON string μΌλ‘ λ³νμν¬ μ μμ΅λλ€.
νΉμ structuredClone() μ΄λΌλ λ°©λ²λ μμ΅λλ€. μ΄ λΆλΆμ μ‘°κΈ λ 곡λΆν νμ ν¬μ€ν νλλ‘ νκ² μ΅λλ€.
μ°Έκ³ μλ£ π
https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy
Deep copy - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
A deep copy of an object is a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made. As a result, when you change either the source or the copy, you can be as
developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
structuredClone() - Web APIs | MDN
The global structuredClone() method creates a deep clone of a given value using the structured clone algorithm.
developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy
Shallow copy - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
A shallow copy of an object is a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made. As a result, when you change either the source or the copy, you may also caus
developer.mozilla.org
'JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
JS λμ¨ν νμ μ λμ μΈμ΄ (0) | 2022.08.11 |
---|---|
JS ν¨μ μ μΈλ¬Έκ³Ό ν¨μ ννμ μ°¨μ΄ (0) | 2022.08.11 |
JS λΆλ³ κ°μ²΄ λ§λ€κΈ° (0) | 2022.08.10 |
JS Truthy μ Falsy (0) | 2022.08.10 |
JS ν λ³ν (λ¬Έμλ₯Ό μ«μλ‘, μ«μλ₯Ό λ¬Έμλ‘), λ³μμ νμ νμΈ typeof (0) | 2022.08.10 |