WEB.JS/06.객체_JS
chapter07 : 객체문제
GAWON
2023. 5. 22. 09:15
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
/*
1.빈 객체 car생성
2.객체 car 동적으로 프로퍼티 추가
1)model = 아무거나
2)speed = 0
3)oil = 50;
4)max_SPEED= 100
5)speedUp 함수 -> speed 10증가시 oil 5 사용
5)speedDown 함수 -> speed 감소에 oil 미사용
```
*/
var car = {};
var car = {
model : "e-class",
speed : 0,
oil : 50,
max_SPEED : 100,
speedUp : function() {
//speed 10증가시 oil 5 사용
if (this.oil >= 5) {
this.speed += 10;
this.oil -= 5;
if (this.speed > this.max_SPEED) {
this.speed = this.max_SPEED
}
}
},
speedDown : function() {
//speed 감소에 oil 미사용
this.speed -= 10;
if (this.speed < 0) {
this.speed = 0;
}
}
};
document.write("<h1>현재 속도" + car.speed + "잔여 기름량" + car.oil + "</h1>");
car.speedUp();
document.write("<h1>현재 속도" + car.speed + "잔여 기름량" + car.oil + "</h1>");
car.speedDown();
document.write("<h1>현재 속도" + car.speed + "잔여 기름량" + car.oil + "</h1>");
car.speedDown();
document.write("<h1>현재 속도" + car.speed + "잔여 기름량" + car.oil + "</h1>");
document.wirte(car);
document.write("<h1>" + car.speedDown() + "</h1>");
document.write("<h1>" + car.max_SPEED + "</h1>");
```
</script>
</head>
<body>
</body>
</html>