π‘ κ°μ²΄μ λ°°μ΄μ μμνμ
μ΄ μλ μ°Έμ‘° reference νμ
- μμ λ€λ£¬ μλ£νλ€μ μμ primitive νμ
- λμ μ°¨μ΄λ λ€μ κ°μμ λ€λ£° κ²
I. κ°μ²΄ 미리보기
- μλ°μ€ν¬λ¦½νΈμμ μμ νμ
μ΄ μλ λͺ¨λ λ°μ΄ν°λ κ·Όλ³Έμ μΌλ‘ κ°μ²΄
- 볡ν©μ μΈ μ 보λ₯Ό νλ‘νΌν° property - (ν€μ κ°μ μ‘°ν©)μΌλ‘ μ μ₯νλ μλ£ν
const objName = {
key1: value1,
key2: value2,
...
};
// β οΈ λΈλ‘μ΄ μλ!
1. νλ‘νΌν° μ κ·Ό
π‘ μμ±κ°μ μ κ·Όνλ λ λ°©λ²
//1. κ°μ²΄λͺ
.key
console.log(
person1.name,
person1.age,
person1.married
);
//2 κ°μ²΄λͺ
[key]
console.log(
person1['name'], // μμ±λͺ
μ stringμΌλ‘
person1['age'],
person1['married'],
);
π‘ μ‘΄μ¬νμ§ μλ ν€λ‘ μ κ·Όμ undefined λ°ν
console.log(person1.birthdate);
console.log(person1['job']);
π‘ (ν€) in (κ°μ²΄) - νΉμ ν€ ν¬ν¨ μ¬λΆ νμΈ
console.log(
'age' in person1,
'job' in person1
);
2. νλ‘νΌν° μμ λ° μΆκ°
// νΉμ νλ‘νΌν°μ κ° λ³κ²½
person1.age = 26;
person1['married'] = true
β
console.log(person1);
// μ νλ‘νΌν° μΆκ°
person1.job = 'developer';
person1['bloodtype'] = 'AB'
β
console.log(person1);
- π‘ constμμλ κ·Έ λ΄μ©μ μμ ν μ μμμ μ£Όλͺ©!
II. λ°°μ΄ λ―Έλ¦¬λ³΄κΈ°
const winners = [12, 592, 7, 48];
const weekdays = ['μ', 'ν', 'μ', 'λͺ©', 'κΈ', 'ν ', 'μΌ'];
β
// μλ£νμ κ΄κ³μμ΄ ν λ°°μ΄μ λ£μ μ μμ
const randoms = ['νκΈΈλ', -24, true, null, undefined];
β
console.log(typeof winners);
console.log(winners, weekdays, randoms);
1. κ°κ³Ό κΈΈμ΄ μ κ·Ό
// νΉμ μμμ κ°μ μ κ·Όνλ λ² (0λΆν° μμ)
console.log(winners[0], weekdays[6], randoms[3]);
// λ°°μ΄μ κΈΈμ΄(μμμ κ°―μ)λ₯Ό μ»λ λ²
console.log(winners.length, weekdays.length, randoms.length);
// λ§μ§λ§ μμ μ»κΈ°
console.log(winners[winners.length - 1]);
2. μμ λ° μΆκ°
const numbers = [1, 2, 3];
β
// νΉμ μμΉμ κ° μμ
numbers[2] = 5;
β
console.log(numbers);
// 맨 λμ κ° μΆκ°
numbers.push(10);
β
console.log(numbers);
- π‘ constμμλ κ·Έ λ΄μ©μ μμ ν μ μμμ μ£Όλͺ©!
- κΈ°ν λ°©λ²λ€μ λ°°μ΄ μΉμ
μμ λ°°μ°κ² λ κ²
π‘ λ°°μ΄μ λ²μ£Ό λλ¨Έλ‘ μ κ·Όμ undefined λ°ν
const winners = [12, 592, 7, 48];
console.log(winners[winners.length]);
βοΈ μ€μ²© μ¬μ© κ°λ₯
const person2 = {
name: 'κΉλ¬μ',
age: 23,
languages: ['Korean', 'English', 'French'],
education: {
school: 'νκ΅λ',
major: ['μ»΄ν¨ν°κ³΅ν', 'μ μ곡ν'],
graduated: true,
}
};
β
console.log(person2.languages[2]);
console.log(person2.education.graduated);
const groups = [[1, 2], [3, 4, 5], [6, 7, 8, 9]];
β
const weapons = [
{ name: 'λ‘±μλ', damage: 30, design: ['ν룑κ²', 'λμ κ²'] },
{ name: 'ν', damage: 12 },
{ name: 'μν΄λ¨Έ', damage: 48 },
];
β
console.log(groups[1][2]);
console.log(weapons[2].damage);
console.log(weapons[0].design[0]);
I. κ° λ³΅μ¬ κ²°κ³Ό λΉκ΅
1. μμ νμ
π MDN λ¬Έμ 보기
- κ°μ μν λ³΅μ¬ copy by value
let number1 = 1;
let string1 = 'ABC';
let bool1 = true;
β
let number2 = number1;
let string2 = string1;
let bool2 = bool1;
β
number2 = 2;
string2 = 'κ°λλ€';
bool2 = false;
β
console.log('~1:', number1, string1, bool1);
console.log('~2:', number2, string2, bool2);
2. μ°Έμ‘° νμ
- μ°Έμ‘°μ μν λ³΅μ¬ copy by reference
const obj1 = {
num: 1, str: 'ABC', bool: true
};
const obj2 = obj1;
// obj2 = {}; // β οΈ μ€λ₯
console.log('obj1:', obj1);
console.log('obj2:', obj2);
// βοΈ constμμλ λ΄λΆ κ°μ λ³κ²½ κ°λ₯ν¨ μ£Όλͺ©
// λ΄λΆ λ³κ²½ λ°©μ§λ μ΄ν λ°°μΈ Object.freeze ν¨μλ‘
obj2.num = 2;
obj2.str = 'κ°λλ€';
obj2.bool = false;
β
console.log('obj1:', obj1);
console.log('obj2:', obj2);
const array1 = [1, 'ABC', true];
const array2 = array1;
// array2 = []; // β οΈ μ€λ₯
console.log('array1:', array1);
console.log('array2:', array2);
// βοΈ constμμλ λ΄λΆ κ°μ λ³κ²½ κ°λ₯ν¨ μ£Όλͺ©
array2[0] = 3;
array2[1] = 'def';
array2[2] = false;
β
console.log('array1:', array1);
console.log('array2:', array2);
II. λ©λͺ¨λ¦¬ μμΈ
1. μμ νμ
let number1 = 1;
number2 = number1;
number2 = 2;
β
console.log(number1, number2);
2. μ°Έμ‘° νμ
a. κ°μ²΄
const obj1 = {
num: 1,
str: 'ABC',
bool: true
};
β
const obj2 = obj1;
β
obj2.num = 2;
β
console.log(obj1, obj2);
b. λ°°μ΄
const array1 = [1, 'ABC', true];
const array2 = array1;
β
array2[1] = 'κ°λλ€';
β
console.log(array1, array2);