2014年9月27日土曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 12(Creating objects: Advanced Object Construction)、EXERCISE(p.544)を解いてみる。

EXERCISE(p.544)

コード(BBEdit, Emacs)

var Dog = function (name, breed, weight) {
        this.name = name;
        this.breed = breed;
        this.weight = weight;
        this.bark = function () {
            if (this.weight > 25) {
                print(this.name + ' says Woof!');
            } else {
                print(this.name + ' says Yip!');
            }
        };
    },
    Cat = function (name, breed, weight) {
        this.name = name;
        this.breed = breed;
        this.weight = weight;
    },
    dogCatcher = function (obj) {
        if (obj instanceof Dog) {
            return true;
        }
        return false;
    },
    meow,
    whiskers,
    fido,
    fluffy,
    spot,
    dogs,
    i,
    max;

meow = new Cat('Mewo', 'Siamese', 10);
whiskers = new Cat('Whiskers', 'Mixed', 12);
fido = {name: 'Fido', breed:'Mixed', weight:30};
fluffy = new Dog('Fluffy', 'Poodle', 30);
spot = new Dog('Spot', 'Chihuahua', 10);
dogs = [meow, whiskers, fido, fluffy, spot];

for (i = 0, max = dogs.length; i < max; i += 1) {
    if (dogCatcher(dogs[i])) {
        print(dogs[i].name + ' is a dog');
    }
}











						

0 コメント:

コメントを投稿