2014年10月6日月曜日

開発環境

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

EXERCISE(p.602)

コード(BBEdit, Emacs)

// constructorプロパティを見やすくするために名前付きで定義。
var Dog = function Dog (name, breed, weight) {
        this.name = name;
        this.breed = breed;
        this.weight = weight;
    },
    ShowDog = function ShowDog (name, breed, weight, handler) {
        this.name = name;
        this.breed = breed;
        this.weight = weight;
        this.handler = handler;
    },
    fido,
    scotty;

ShowDog.prototype = new Dog();
ShowDog.prototype.constructor = ShowDog;

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) { // false
    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










						

0 コメント:

コメントを投稿