Head First JavaScript 第五章:5 对象

2019-12-02

17:14:30

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Car properties</title>
<script>

var fiat = { 
    make: "Fiat",
    model: "500",
    year: 1957, 
    color: "Medium Blue",
    passengers: 2,
    convertible: false,
    mileage: 88000,
    started: false,

    start: function() {
        this.started = true;
    },

    stop: function() {
        this.started = false;
    },

    drive: function() {
        if (this.started) {
            alert(this.make + " " +
                    this.model + " goes zoom zoom!");
        } else {
            alert("You need to start the engine first.");
        }
    }
};

fiat.start();
fiat.drive();

for (prop in fiat) {//迭代对象中的每个属性
    console.log(prop + ": " + fiat[prop]);
}

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

 

 

posted @ 2019-12-03 10:17  JasonPeng1  阅读(108)  评论(0编辑  收藏  举报