저는 지붕, 차고 및 주택에 대한 세 가지 클래스로 JavaScript를 작성하고 있습니다. 하우스 클래스는 생성자 인 Roof and Garage에 두 개의 인수를 취합니다. 이 코드를 실행하면 내가 얻을 :인수가 다른 객체 인 JavaScript 객체 생성자와 관련된 문제
('객체를 생성 할 수 없습니다') 새로운 오류가 발생 오브젝트 [이 오류에 브레이크]를 만들 수 없습니다 \ n 방화범에
개체가 명확에도 불구하고 올바른 유형의 내가 뭘 잘못하고 있는거야? 여기 코드는 다음과 같습니다
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());
네가 맞아! 그렇다면 올바른 종류의 객체가 생성자에 전달되었는지 확인하려면 어떻게해야할까요? 나는 지붕을 기대하고있을 때 누군가 Foo 객체를 넘기는 것을 원하지 않을 것입니다 ... – Ralph