我正在编写一些带有三个类的 JavaScript,一个用于屋顶,一个用于车库,一个用于房屋。house 类的构造函数有两个参数,一个 Roof 和一个 Garage。当我运行此代码时,我得到:
无法构造对象 [Break on this error] throw new Error('can notconstruct object');\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());