【问题标题】:Javascript call to class method CalculateCost() does not work correctly. Not returning any information对类方法 CalculateCost() 的 Javascript 调用无法正常工作。不返回任何信息
【发布时间】:2014-10-08 23:11:36
【问题描述】:

我有一个名为 PortraitOrder() 的类构造函数。在这个构造函数中,我创建了一个名为 CalculateCost() 的方法。出于某种原因,当我稍后在我的代码中调用该方法时,根本没有返回任何内容。我不确定是我调用它的方式、我在 PortraitOrder() 中创建它的方式还是实际的 CalculateCost() 方法本身存在问题。任何和所有建议/建议将不胜感激。

  1. 这是带有方法声明的 PortraitOrder() 类。

       function PortraitOrder(portrait, copies, size, buyer){
    
                 this.portrait = portrait;
                 this.copies = copies;
                 this.size = size;
                 this.buyer = buyer;
                 this.calculateCost = CalculateCost;
    
    
    
    
        }
    
  2. 这是我在 PortraitOrder() 下方添加的 CalculateCost() 方法

       function CalculateCost(){
    
                 this.size = size;
    
                 this.copies = copies;
    
                 var price = 0;
    
                 if(size == "wallet"){
    
                      price = 10;
    
                 }
                 else if(size == "8x10"){
    
                      price = 10;
    
                 }
                 else if(size == "4x6"){
    
                      price = 10;
    
                 }
                 else if(size == "11x14"){
    
                      price = 30;
    
                 }
                 else{
    
                      price = 10;
    
                 }
    
                var cost = price * copies;
    
                return cost;
    
                 }
    
  3. 最后,这是我对该方法的调用。 Portraits 是我之前在代码中创建的类的实例的名称。

              document.write("Order cost: " + portraits.calculateCost());
    

【问题讨论】:

  • 因为你不使用价格。大声朗读你的代码。
  • 刚刚修复。仍然没有得到任何东西。
  • 你怎么称呼它? document.write 也是一个糟糕的选择。
  • 嗯,this.size = size; this.copies = copies; 是错误的方式。应该是var size = this.size, copies = this.copies;
  • 我使用 document.write 的原因是因为它在一个单独的 .js 文件中,并且它正在写入一个 html 页面。另外,我写了我如何称呼它。

标签: javascript class methods call


【解决方案1】:

您的代码可能在这里抛出异常:

function CalculateCost() {

         this.size = size;  //size is unknown to interpreter here

         this.copies = copies;  //copies is unknown to interpreter here

         ...
}

一种更简洁、更直接的方式来做所有事情(也请记住,就像 epascarello 所说,你没有使用 price):

function PortraitOrder(portrait, copies, size, buyer) {

         this.portrait = portrait;
         this.copies = copies;
         this.size = size;
         this.buyer = buyer;
         this.calculateCost = function() {

         var price = 0;

         if(size == "wallet"){

              price = 10;

         }
         else if(size == "8x10"){

              price = 10;

         }
         else if(size == "4x6"){

              price = 10;

         }
         else if(size == "11x14"){

              price = 30;

         }
         else{

              price = 10;

         }

        var cost = size * copies;

        return cost;

     }

}

【讨论】:

  • 我将我的代码更改为那个,我用这个创建的页面以及我的其他代码完全停止加载。
  • 等等,忘了我说过的。我省略了一个括号。页面正在显示。但是,它返回“订单成本:NaN”
  • 尝试将var cost 行更改为var cost = this.size * this.copies;。如果这不起作用,请在该行设置调试器,然后查看 sizecopies 的实际值。
  • 就是这样!!!我早些时候将该行中的大小更改为价格,但我将副本更改为 this.copies。现在很好用!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
  • 2014-01-05
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
相关资源
最近更新 更多