屋根用、ガレージ用、住宅用の 3 つのクラスを持つ JavaScript を作成しています。house クラスは、コンストラクターに Roof と Garage の 2 つの引数を取ります。このコードを実行すると、次のようになります。
オブジェクトを構築できません [このエラーで中断] 新しいエラーをスローします('オブジェクトを構築できません');\n
オブジェクトが明らかに正しいタイプであっても、Firebug で。私が間違っていることは何か分かりますか?コードは次のとおりです。
function Roof(type, material) {
this.getType = function() { return type; }
this.getMaterial = function() { return material; }
}
function Garage(numberOfCars) {
this.getNumberOfCars = function() { return numberOfCars; }
}
function House(roof, garage) {
if (typeof roof !== 'Roof' || typeof garage !== 'Garage') {
throw new Error('can not construct object');
}
this.getRoof = function() { return roof; }
this.getGarage = function() { return garage; }
}
myRoof = new Roof("cross gabled", "wood");
myGarage = new Garage(3);
myHouse = new House(myRoof, myGarage);
alert(myHouse.getRoof().getType());