JavaScript 对象构造函数的问题,其中参数是其他对象

发布于 2024-08-07 13:49:40 字数 915 浏览 8 评论 0原文

我正在编写一些包含三个类的 JavaScript,一个用于屋顶,一个用于车库,一个用于房屋。 house 类的构造函数有两个参数:屋顶和车库。当我运行此代码时,我得到:

无法构造对象 [Break on this error] throw new Error('can not Construction 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());

I'm writing some JavaScript with three classes, one for Roofs, one for Garages, and one for Houses. The house class takes two arguments to its constructor, a Roof and a Garage. When I run this code I get:

can not construct object [Break on this error] throw new Error('can not construct object');\n

in Firebug even though the objects are clearly of the right type. Any idea what I'm doing wrong? Here's the code:

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());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岛徒 2024-08-14 13:49:40

typeof 运算符将为您的对象返回 "object",而不是它们的名称。请参阅typeof Operator 文档

function House(roof, garage) {
    alert(typeof roof);   // "object"
    ...

您可能需要 instanceof

function House(roof, garage) {
    if (!(roof instanceof Roof) || !(garage instanceof Garage)) {
    ...

The typeof operator will return "object" for your objects, not their names. See the typeof Operator documentation.

function House(roof, garage) {
    alert(typeof roof);   // "object"
    ...

You probably want instanceof:

function House(roof, garage) {
    if (!(roof instanceof Roof) || !(garage instanceof Garage)) {
    ...
就此别过 2024-08-14 13:49:40

myRoofmyGarageobject 类型。

如果您想检查 myRoof 是否是 Roof 的实例,请使用 isinstanceof。

>>myRoof isinstanceof Roof
True

myRoof and myGarage are object types.

If you want to check if myRoof is an instance of Roof, use isinstanceof.

>>myRoof isinstanceof Roof
True
謌踐踏愛綪 2024-08-14 13:49:40

正如 Richie 所指出的,typeof 将返回“对象”,而不是函数的名称。 您应该使用“构造函数”属性。使用“instanceof”运算符。

另外,我使用了两个“if 语句”(而不是像您那样使用一个)来根据特定错误抛出不同的错误消息。这可能意味着需要更多的代码,但是当代码损坏时,您可以确切地知道出了什么问题。

工作演示 →

代码:

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 (roof instanceof Roof)
     {
        throw new Error('Argument roof is not of type Roof');
     }

     if(garage instanceof Garage) 
     {
          throw new Error('Argument garage must be of type Garage.');
     }

     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());

As noted by Richie, typeof will return 'object', not the name of the function. You should use the 'constructor' property. Use 'instanceof' operator.

Also, I have used two 'if statements' (instead of one, like you did) to throw different error message based on the particular error. This may mean a little more code, but when the code breaks, you know exactly what went wrong.

Working demo →

Code:

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 (roof instanceof Roof)
     {
        throw new Error('Argument roof is not of type Roof');
     }

     if(garage instanceof Garage) 
     {
          throw new Error('Argument garage must be of type Garage.');
     }

     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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文