본문 바로가기

Javascript

Javascript 객체 속성다루기

반응형

1.  데이터를 담아내는 그릇이 객체다. 

 

2. 배열은 인덱스가 숫자다. 객체는 인덱스가 문자다. 

ex: const person = {name:"hong gil dong", age:35, height: 180}

 

3. alert(); 를 사용하면 앞에있는 매개변수만 인식하여 출력한다. 뒤에 있는 person["name"]은 출력x... 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
<script>
  const person = {
    name:"Hong Gildong",
    age:20
  };
  console.log(person["name"]); 
  console.log(person["age"]); 
  
  alert(person["age"], person["name"]);

</script>
</body>
</html>

4. 속성값을 확인하기 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
<script>
  const person = {
    name:{
      firstName:"gildong",
      lastName:"Hong"
    },
    likes:["apple", "samsung"],
    printHello:function(){
      return "hello";
    }
  };

  console.log(person["name"]);
  console.log(person["name"]["firstName"]);
  console.log(person["likes"][1]);
  console.log(person["likes"][0]);
  console.log(person["printHello"]);
  console.log(person["printHello"]());
</script>
</body>
</html>

 

 

반응형