tddjs > 継承
誤り step1
by s_hiroshi
JavaScript
function Normal(value) {
this.power = 100;
if (value === undefined) {
throw {
message: "引数が必要"
};
}
else {
this.name = value;
}
}
Normal.prototype.getName = function() {
return this.name;
};
Normal.prototype.getPower = function() {
return this.power;
};
function Turbo(value) {
this.power = 150;
if (value === undefined) {
throw "引数が必要"
}
else {
this.name = value;
}
}
// Normal.prototypeのオブジェクト参照が渡る
Turbo.prototype = Normal.prototype;
// Normal.prototypeを書き換えてしまう
Turbo.prototype.powerup = 50;
try {
var na = new Normal('normal');
console.log(na.powerup); // 50 undefinedにならない
console.log(na instanceof Turbo); // true
} catch (e) {
console.log(e.message);
}