this关键字制定对象的属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Cars</title>
    <script>
        //每次引用属性started时,都在它前面加上this和句点
        //告诉Javascript解释器,你说的是当前对象的属性started
        //以免Javascript以为你引用的是一个变量
        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("Zoom, Zoom!");
                } else {
                    alert("You need to start the engine first.");
                }
            }
        };
        fiat.drive();
        fiat.start();
        fiat.drive();
        fiat.stop();
    </script>
</head>
<body>

</body>
</html>

 

posted @ 2018-07-14 18:40  道高一尺  阅读(138)  评论(0)    收藏  举报