【问题标题】:Issue with JavaScript object constructor where arguments are other objectsJavaScript 对象构造函数的问题,其中参数是其他对象
【发布时间】:2009-10-10 22:58:57
【问题描述】:

我正在编写一些包含三个类的 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());

【问题讨论】:

    标签: javascript constructor object instanceof typeof


    【解决方案1】:

    typeof 运算符将为您的对象返回"object",而不是它们的名称。见the typeof Operator documentation

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

    你可能想要instanceof:

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

    【讨论】:

    • 你是对的!那么如何确保将正确类型的对象传递给构造函数呢?当我期待一个屋顶时,我不希望有人传入一个 Foo 对象......
    【解决方案2】:

    myRoofmyGarageobject 类型。

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

    >>myRoof isinstanceof Roof
    True
    

    【讨论】:

      【解决方案3】:

      正如 Richie 所说,typeof 将返回“对象”,而不是函数的名称。 你应该使用'constructor'属性。使用“instanceof”运算符。

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

      Working demo →

      代码:

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

      【讨论】:

      • Bobice,你是对的,最好使用 instanceof。更正了答案。我不应该急于回答问题! :)
      • 不要使用constructor。它不是标准的,在 IE 中不可用,而且一旦你开始使用原型,它就不会按照你的想法去做。在 JavaScript 中测试对象继承的唯一标准、可靠的方法是 instanceof
      • 哎呀,时间异常! (咳咳)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2018-10-07
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多