Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ—ž 얕은볡사 κΉŠμ€λ³΅μ‚¬ κ°œλ… #18

Open
leemember opened this issue Jul 4, 2022 · 0 comments

Comments

@leemember
Copy link
Owner

4. 얕은볡사 κΉŠμ€λ³΅μ‚¬ κ°œλ…

🎈4-1. 얕은볡사 (shallow copy)λž€ ?

얕은볡사

const obj1 = { a : b, b : 2 };
const obj2 = obj1;
 
console.log( obj1 === obj2 ); // true
console.log(obj1); // {a: 1, b: 2}
console.log(obj2); // {a: 1, b: 2}

이 μ˜ˆμ‹œμ²˜λŸΌ 객체λ₯Ό 직접 λŒ€μž…ν•˜λŠ” 경우 참쑰에 μ˜ν•œ 할당이 μ΄λ£¨μ–΄μ§€λ―€λ‘œ λ‘˜μ€ 같은 데이터 (μ£Όμ†Œ)λ₯Ό 가지고 μžˆλ‹€. 이것을 얕은 볡사라고 ν•œλ‹€.

🎈4-2. κΉŠμ€ 볡사 (Deep copy)λž€ ?

κΉŠμ€λ³΅μ‚¬

const dog = {a: 1, b: 2};
const cat = {...dog};
cat.a = 100;
console.log(dog === cat) // false
console.log(cat.a) // 1

...(spread) μ—°μ‚°μžλ₯Ό 톡해 블둝 μ•ˆμ— dog의 속성을 λ³΅μ‚¬ν•˜λ©° cat에 ν• λ‹Ήν•˜μ˜€λ‹€. 이제 dog와 cat은 λ‹€λ₯Έ μ£Όμ†Œλ₯Ό κ°–κ²Œ λ˜μ—ˆλ‹€. λ©”λͺ¨λ¦¬ μ°Έμ‘°κ°€ λ‹€λ₯Έ 것.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant