ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Javascript] 객체, λ°°μ—΄, μ›μ‹œνƒ€μž…, μ°Έμ‘°νƒ€μž…
    μ›Ή/JavaScript 2023. 1. 10. 11:54

    πŸ’‘ 객체와 배열은 μ›μ‹œνƒ€μž…μ΄ μ•„λ‹Œ μ°Έμ‘° 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);

Designed by Tistory.