2014年10月1日水曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 13(Extra strength objects: Using Prototypes)、EXERCISE(p.583)を解いてみる。

EXERCISE(p.583)

コード(BBEdit, Emacs)

var Game = function () {
        this.level = 0;
    },
    Robot = function (name, year, owner) {
        this.name = name;
        this.year = year;
        this.owner = owner;
    },
    game,
    robby,
    rosie;

Game.prototype.play = function () {
    this.level += 1;
    print('Welcome to level ' + this.level);
    this.unlock();
};

Game.prototype.unlock = function () {
    if (this.level === 42) {
        Robot.prototype.deployLaser = function () {
            print(this.name + ' is blasting you with laser beams.');
        };
    }
};

game = new Game();
robby = new Robot('Robby', 1956, 'Dr. Morbius');
rosie = new Robot('Rosie', 1962, 'George Jetson');

while (game.level < 42) {
    game.play();
}

robby.deployLaser();
rosie.deployLaser();










						

0 コメント:

コメントを投稿