[JavaScript] 객체(Object)
포스트
취소

[JavaScript] 객체(Object)

#객체(Object)

자바스크립트의 객체는 {키:값}로 표기됩니다.
자바스크립트의 객체는.을 찍어 접근한다. Java의 Map과 비슷하다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<script>
    //객체 생성
    var person ={name: "홍길동", age: 20, arr:[1,2,3]};

    //객체에 접근하는 첫번째 방법
    console.log(person.name);
    console.log(person.age);
    console.log(person.arr[0]);
    console.log(person);

    //객체에 접근하는 두번째 방법
    console.log(person["name"]);
    console.log(person["arr"][0]);


    var exam = {math:50, eng:70, kor:90};

    var kim = {name:"김길동", exam: exam};
    //위의 kim 객체의 exam key 값에 위 exam 객체가 저장됨
    console.log(kim.name);
    console.log(kim.exam);
    //실행결과 {name: '홍길동', age: 20, arr: Array(3)}
    
    //배열안에 객체
    var arr =[
        {id:1, title:"hi", content:"안녕하세요"},
        {id:2, title:"bye", content:"안녕히가세요"}
    ];

    console.log(arr);
    console.log(arr[0].id);
    console.log(arr[0].title);
    console.log(arr[0].content);
</script>
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.