WEB.JS/06.객체_JS

chapter02 : 객체

GAWON 2023. 5. 19. 17:40
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
//1. 객체 생성
var car = {
model : "e-class",
price : 8000,
company : { //중괄호는 객체! 객체 안에 객체가 있는 형태!

name : "benz",
logo : "triangle"
},
tires : [ "frontLeft", "fronRight", "backLeft", "backRight" ], //대괄호는 배열!
drive : function() {
document.write("Let's go!");
}
};

```
// 2. 객체 출력
   document.write("<h1>");

   document.write(car.model + "<br>");
   document.write(car["price"] + "<br>");

   document.write(car.company["name","logo"] + "<br>");
   document.write(car["company"]["name"] + "<br>");
   document.write(car.company.logo + "<br>");

   document.write(car.tires + "<br>");
   for (var i = 0; i < car.tires.length; i++) {
      document.write(car.tires[i] + "<br>");
   }

   car.drive();

   document.write("</h1>");

```

</script>
</head>
<body>

</body>
</html>