ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Object (2/9, 2/10)
    Web front/JavaScript 2022. 2. 10. 01:58

    1/ object 다양한 활용 방법

    let hunbu = new Object();
    hunbu.name = '흥부';
    hunbu.kor = 100;
    hunbu.eng = 95;
    hunbu.total = hunbu.kor + hunbu.eng;
    hunbu.avg = hunbu.total / 2;
    console.log(`${hunbu.name}님의 총점은 ${hunbu.total} / ${hunbu.avg}`);
    
    // new object 사용안하고 바로 가능
    let angie = {
        name: '앤지',
        kor: 100,
        eng: 90,
        total: this.kor + this.eng,		// this 아닌데 뭐지,,?
        avg: this.total / 2,
    }
    console.log(angie);
    // 구조 파악
    console.dir(angie);
    
    
    // object는 key : value 쌍의 집합니다.
    const jonny = {
        name: '죠니',
        'jonny-kor': 100,
        'jonny-eng': 88,
        100: 150,
    }
    console.log(jonny);
    console.log(jonny['jonny-kor']);
    console.log(jonny['100']);
    
    
    const hong = {
        name: '홍길동',
        kor: 100,
        eng: 90,
        total: function() {
            return this.kor + this.eng;
        },
        avg: function(num) {
            return this.total() / num;
        }
    }
    console.log(hong);
    console.log(hong.total());
    console.log(hong.avg(2));

     

    2/ const는 변경 불가.

    const 타입인 arr의 각 요소는 삭제 및 수정 가능하다. 왜냐면 변경할 수 없는건 arr를 가르키는 주소이기 때문에.

    그래서 arr를 새로 정의하려는 코드는 에러. 주소를 바꾸려고 하니까. 

    const arr = [10, 20, 30];
    arr[0] = 100;
    console.log(arr);
    
    arr = [];	//error

     

     

    3/ destructing : 배열에서 값 꺼내서 다른 변수에 대입하는 것

    var ary = [10, 11, 100, 101, 1000, 1001];
    const [a, b, c] = ary;
    console.log(a, b, c);
    
    const [ , , d, e, f] = ary;
    console.log(d, e, f);
    
    const [g, h, , i] = ary;
    console.log(g, h, i);
    
    const [j, k, l, ...rest] = ary;
    console.log(j, k, l, rest);
    
    const [m, n, [o, p]] = [10, 11, [100, 101]];
    console.log(m, n, o, p);
    
    
    let x = 10;
    let y = 20;
    [y, x] = [x, y]
    console.log(x, y);
    console.log('');
    console.log('===============================');
    
    var objArray = [
        {id: 1, name: 'NolBu', age: 35}, 
        {id: 2, name: 'BangJa', age: 18}, 
        {id: 3, name: 'HungBu', age: 25}
    ];
    
    let z;
    [x, y, z] = objArray;
    console.log(x);
    console.log(y);
    console.log(z);
    console.log('');

    'Web front > JavaScript' 카테고리의 다른 글

    Variable (2/8)  (0) 2022.02.08
Designed by Tistory.