2014年8月17日日曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 5(Understanding Objects: A trip to Objectville)、EXERCISE(p.212)を解いてみる。

EXERCISE(p.212)

コード(BBEdit, Emacs)

var fiat = {
        make: "GM",
        model: "Cadillac",
        year: 1955,
        color: "tan",
        passengers: 5,
        convertible: false,
        mileage: 12892,
        started: false,
        fuel: 0,
        start: function () {
            if (this.fuel > 0) {
                this.started = true;
            } else {
                print('The car is on empty, fill up before starting!');
            }
        },
        stop: function () {
            this.started = false;
        },
        drive: function () {
            if (this.started) {
                if (this.fuel > 0) {
                    print(this.make + ' ' + this.model + ' goes zoom zoom!');
                    this.fuel -= 1;
                } else {
                    print("Uh oh, out of fuel.");
                    this.stop();
                }
            } else {
                print('You need to start the engine first.');
            }
        },
        addFuel: function (amount) {
            this.fuel += amount;
        }
    };

fiat.start();
fiat.drive();
fiat.addFuel(2);
fiat.start();
fiat.drive();
fiat.drive();
fiat.drive();
fiat.stop();












						

0 コメント:

コメントを投稿