javascript reference and copy variables - Lee-hyuna/33-js-concepts-kr GitHub Wiki
μλ¬Έ: https://hackernoon.com/javascript-reference-and-copy-variables-b0103074fdf0
κ° νλ‘κ·Έλλ° μΈμ΄μλ κ³ μ ν νΉμ±μ΄ μμ§λ§ μλ°μ€ν¬λ¦½νΈλ λ§μ νΉμ±μ κ°μ§κ³ μλ€. ν΄λΉ ν¬μ€ν μ μ°Έμ‘°λ‘ ν λΉλ λ³μμ 볡μ¬μ μν΄ ν λΉλ λ³μμ λν΄ μ΄μΌκΈ°ν κ²μ΄λ€.
μ°μ μλ°μ€ν¬λ¦½νΈμ μμ νμ κ°λ€μ μ΄ν΄λ³΄μ.
null
undefined
String
Number
Boolean
Object
Array
Function
κΈ°μ μ μΌλ‘ μμ νμ μ νμ Pass-by-value(볡μ¬)μ΄λ©° λ³΅ν© νμ μ Pass-by-reference(μ°Έμ‘°)μ΄λ€. μλ₯Ό λ€μ΄λ³΄μ.
let person1 = 'Michael';
let person2 = person1;
person1 = 'John';
console.log(person1); // John
console.log(person2); // Michael
let birthYear1 = 1995;
let birthYear2 = birthYear1;
birthYear1++;
console.log(birthYear1); // 1995
console.log(birthYear2); // 1996
μμ μμ λ μμ νμ μΌλ‘ κ°μ 볡μ¬νκ³ , λ³΅μ¬ λμμ΄λλ κ°(person1, birthYear1)μ λ³κ²½νλ€. λ³΅μ¬ λμμ΄ λλ κ°μ λ³κ²½νμ¬λ 볡μ¬ν κ°μλ μν₯μ λ―ΈμΉμ§ μλλ€. κ·Έλ¬λ λ³΅ν© νμ μ κ²½μ°, λ€μκ³Ό κ°μ κ²μ΄λ€.
let inventory = ['banana', 'apple'];
let purchased = inventory;
inventory.push('strawberry');
purchased[1] = 'corn';
console.log(inventory); // ["banana", "corn", "strawberry"]
console.log(purchased); // ["banana", "corn", "strawberry"]
let person1 = { name: 'Vitor', birthYear: 1995 };
let person2 = person1;
person2.name = 'Eduardo';
person1.birthYear = 1992;
console.log(person1); // Object {name: "Eduardo", birthYear: 1992}
console.log(person2); // Object {name: "Eduardo", birthYear: 1992}
λ³΅ν© νμ (Object, Array)λ₯Ό μ¬μ©νμ¬ λ³΅μ¬ λμμ΄ λλ κ°(inventory, person1)μ λ³κ²½νμ¬λ 볡μ¬ν κ°(purchased, person2)μ μν₯μ λ―ΈμΉκ³ , κ·Έ λ°λμ κ²½μ°μλ μν₯μ λ―ΈμΉκ³ μλ€.
let inventory = ['banana', 'apple'];
let purchased = inventory;
inventory = ['pie', 'bacon', 'pizza'];
purchased[1] = 'corn';
console.log(inventory); // ["pie", "bacon", "pizza"]
console.log(purchased); // ["banana", "corn"]
let person1 = { name: 'Vitor', birthYear: 1995 };
let person2 = person1;
person1 = { name: 'Eduardo', birthYear: 1992 };
console.log(person1); // Object {name: "Eduardo", birthYear: 1992}
console.log(person2); // Object {name: "Vitor", birthYear: 1995}
μ μμ μμ μ λ³΅μ¬ λμμ κ°κ³Ό 볡μ¬λ κ°μ μλ‘ κ°μ΄ λ€λ₯ΌκΉ? λΆλͺ Objectμ Arrayλ μ°Έμ‘° 볡μ¬κ° λλ κ°μΌν λ°..?
μλ°μ€ν¬λ¦½νΈλ μ€μ λ‘ μ°Έμ‘° κ°μ 볡μ¬νκ³ μκ³ , Object λλ Arrayμ μμ±(property)μ λ³κ²½ν΄λ μ°Έμ‘°λ₯Ό λ³κ²½νμ§λ μλλ€. (μμ μμ μ purchased κ°μ νμΈνμ)
νμ§λ§ λ³μμ μλ‘μ΄ κ°μ μ§μ νλ©΄ μ°Έμ‘° μ체λ₯Ό λ³κ²½νκ³ μλ Object/Arrayμ μν₯μ λ―ΈμΉμ§ μλλ€! (μμ μμ μ inventory κ°μ νμΈνμ)
μλ³Έ κ°μ λν λ³κ²½μ κ±±μ νμ§ μκ³ Objectμ μμ±μ μ λ°μ΄νΈ νκ³ λ°°μ΄μ μμ νκ³ μΆλ€κ³ κ°μ ν΄λ³΄μ.
μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ €λ©΄ 볡μ¬λ³Έμ λ§λ€λ©΄ λλ€! λ€μμ μμ μ΄λ€. (ES6 λ²μ μμλ§ μ¬μ©κ°λ₯).
let inventory = ['banana', 'apple'];
// ES5 >> let purchased = inventory.slice(); // OR let purchased = [].concat(inventory);
// ES6 methods
let purchased = [...inventory]; // OR let purchased = Array.from(inventory); μλ‘μ΄ κ°μΌλ‘ 볡μ¬
inventory.push('strawberry');
purchased[1] = 'corn';
console.log(inventory); // ["banana", "apple", "strawberry"]
console.log(purchased); // ["banana", "corn"]
let person1 = { name: 'Vitor', birthYear: 1995 };
// ES6
let person2 = Object.assign({}, person1); // μλ‘μ΄ κ°μΌλ‘ 볡μ¬
person2.name = 'Eduardo';
person1.birthYear = 1992;
console.log(person1); // Object {name: "Vitor", birthYear: 1992}
console.log(person2); // Object {name: "Eduardo", birthYear: 1995}
ES6μ ...(spread operator)
μ Object.assign λ©μλλ₯Ό μ΄μ©νμ¬ λ°°μ΄κ³Ό κ°μ²΄μ κ°μ 볡μ¬νμλ€.
μ΄μ μλ³Έ κ°μ΄ λ³κ²½λλ μ μ κ±±μ ν νμκ° μλ€! λ κ°μ μλ³Έ κ°μ 볡μ¬νλλ°, μλ³Έ κ°μ΄ λ³κ²½λ κ²½μ° μ΄ μ루μ μ μ¬μ©νλ€.
μ΄ ν¬μ€ν μ΄ λ΄κ° μ΄ μ¬μ€μ μμμ λ λ§νΌμ΄λ λ μλ€μκ² μ μ©νμΌλ©΄ μ’κ² λ€! μλ°μ€ν¬λ¦½νΈλ₯Ό λ λ°°μ°κ³ λ°°μ΄ κ²μ λν΄ κΈμ μ°λ κ²μ κ³μνκ³ μλ€. κ·Έλ¬λ μ¬κΈ° Mediumμ νΈμν°μμ νλ‘μ νλ κ²μ μμ§ λ§μμΌλ©΄ μ’κ² λ€!
μ’μμ λ²νΌμ ν΄λ¦νκ³ μλ°μ€ν¬λ¦½νΈλ νΌλλ°±μ λν΄ μκ³ μΆμ λ΄μ© μλμ μ견μ μΈκΈν¨μΌλ‘μ¨ βRecommendβλ₯Ό μ£Όλ κ²μ΄ μ’λ€λ©΄ λ§μ΄λ€!
- μλ°μ€ν¬λ¦½νΈμ κΈ°μ΄μ΄μ§λ§ κ°λ°νλ©΄μ κ°λ κΉλ¨Ήλ μ°Έμ‘° 볡μ¬
- λ΄μ©μ΄ μ½κ° λΉμ½ν κ² κ°μ§λ§ κΈ°λ³Έ μ μΈ λ΄μ©μ λ€μ΄μλ€.