2014年10月5日日曜日

開発環境

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

EXERCISE(p.600)

コード(BBEdit, Emacs)

var Dog = function (name, breed, weight) {
        this.name = name;
        this.breed = breed;
        this.weight = weight;
    },
    ShowDog = function (name, breed, weight, handler) {
        this.name = name;
        this.breed = breed;
        this.weight = weight;
        this.handler = handler;
    },
    fido,
    scotty;

ShowDog.prototype = new Dog();

fido = new Dog('Fido', 'Mixed', 38);
if (fido instanceof Dog) { // true
    print('Fido is a Dog');
}
if (fido instanceof ShowDog) { // false
    print('Fido is a ShowDog');
}

scotty = new ShowDog('Scotty', 'Scottish Terrier', 15, 'Cookie');
if (scotty instanceof Dog) { // true
    print('Scotty is a Dog');
}
if (scotty instanceof ShowDog) { // true
    print('Scotty is a ShowDog');
}

print('Fido constructor is ' + fido.constructor); // Dog
print('Scotty constructor is ' + scotty.constructor); // ShowDog
// ShowDogだと思ったら、Dogだった。










						

0 コメント:

コメントを投稿