<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>面向对象编程思路</title>
</head>
<body>
</body>
<script type="text/javascript">
// 把大象放进冰箱,需要几个步骤
/*
// ------------- 面向过程的编程思路 -------------
// false : 门是关闭状态 true : 门是开启的状态
var door = false;
// false : 冰箱无物 true : 冰箱有物
var box = false;
// 第一步 : 打开冰箱的门
function openDoor(){
door = true;
}
openDoor();
// 第二步 : 把大象放进去
function putElephant(){
if(door){
if(box){
box = false;
}
if(!box){
box = true;
}
}
}
putElephant();
// 第三步 : 把门关上
function closeDoor(){
if(door){
door = false;
}
}
closeDoor();
*/
// ---------------- 面向对象的编程思路 ------------------
function Box(door, box) {
this.door = door;
this.box = box;
}
Box.prototype.openDoor = function() {
this.door = true;
return this;
}
Box.prototype.putElephant = function() {
if(this.door) {
this.box = true;
}
return this;
}
Box.prototype.closeDoor = function() {
if(this.door) {
this.door = false;
}
return this;
}
// 调用
var box = new Box(false, false);
box.openDoor();
box.putElephant();
box.closeDoor();
</script>
</html>