原型功能不再起作用,出现未定义的状态

时间:2018-06-19 21:27:13

标签: javascript arrays string

我不知道我做错了什么。在我上载到github.io之前,它起作用了,所以我怀疑我弄乱了一些分支,现在卡在了一个旧文件中。我不可能被困住,只需要一个外部的角度就可以解决这个问题。

这是一个帮助订购比萨饼的程序(全部在js / jquery中),它不再累加变量并返回订单总额。它说 var total is undefined >和我不确定是我的方程式还是变量。我之前做得还不错,所以我知道我的代码是可能的,我认为我在操作过程中的某个地方犯了一个错误切换/添加使它变得混乱的分支。

function order(sidesOrderedReduced, pizzaSauceReduced, beveragesOrderedReduced, dessertsOrderedReduced, pizzaToppingsReduced, zipcodeAddress, cityAddress, streetAddress, total){
  this.sidesOrderedReduced = sidesOrderedReduced;
  this.beveragesOrderedReduced = beveragesOrderedReduced;
  this.dessertsOrderedReduced = dessertsOrderedReduced;
  this.pizzaSauceReduced = pizzaSauceReduced;
  this.pizzaToppingsReduced = pizzaToppingsReduced;
  this.zipcodeAddress = zipcodeAddress;
  this.cityAddress = cityAddress;
  this.streetAddress = streetAddress;
  this.total = total;
}

var sidesListed = [];
var sidesOrdered = [];
sidesOrderedReduced = sidesOrderedReduced || 0
var sidesOrderedReduced;
var beveragesListed= [];
var beveragesOrdered = [];
var beveragesOrderedReduced;
beveragesOrderedReduced = beveragesOrderedReduced || 0
var dessertsListed = [];
var dessertsOrdered = [];
var dessertsOrderedReduced;
dessertsOrderedReduced = dessertsOrderedReduced || 0

var pizzaSauceListed = [];
var pizzaSauce = [];
var pizzaSauceReduced = [];
pizzaSauceReduced = pizzaSauceReduced || 0
var pizzaToppingsListed = [];
var pizzaToppingsOrdered = [];
var pizzaToppingsReduced;
pizzaToppingsReduced = pizzaToppingsReduced || 0

var total;

var zipcodeAddress = $("input#zipcodeAddress").val();
var cityAddress = $("input#cityAddress").val();
var streetAddress = $("input#streetAddress").val();

var newOrder = new order(sidesOrderedReduced, beveragesOrderedReduced, dessertsOrderedReduced, pizzaSauceReduced, pizzaToppingsReduced, zipcodeAddress, cityAddress, streetAddress, total);

order.prototype.total = function(){
  return this.beveragesOrderedReduced+this.sidesOrderedReduced+this.dessertsOrderedReduced+this.pizzaSauceReduced+this.pizzaToppingsReduced;
}

  $("input:checkbox[value=sides]:checked").each(function(){
    sidesListed.push($(this).attr("id"));
    sidesOrdered.push(5);
    sidesOrderedReduced =  sidesOrdered.reduceRight(function(a,b){return a+b;});
    return sidesOrderedReduced;
  });

  $("input:checkbox[value=pizzaSauce]:checked").each(function(){
    pizzaSauceListed.push($(this).attr("id"));
    pizzaSauce.push(10);
    pizzaSauceReduced =  pizzaSauce.reduceRight(function(a,b){return a+b;});
    return pizzaSauceReduced;
  });

  $("input:checkbox[value=pizzaToppings]:checked").each(function(){
    pizzaToppingsListed.push($(this).attr("id"));
    pizzaToppingsOrdered.push(1);
    pizzaToppingsReduced = pizzaToppingsOrdered.reduceRight(function(a,b){return a+b;});
    return pizzaToppingsReduced;
  });

  $("input:checkbox[value=beverages]:checked").each(function(){
    beveragesListed.push($(this).attr("id"));
    beveragesOrdered.push(3);
    beveragesOrderedReduced = beveragesOrdered.reduceRight(function(a,b){return a+b;});
    return beveragesOrderedReduced;
  });

  $("input:checkbox[value=desserts]:checked").each(function(){
    dessertsListed.push($(this).attr("id"));
    dessertsOrdered.push(5);
    dessertsOrderedReduced = dessertsOrdered.reduceRight(function(a,b){return a+b;});
    return dessertsOrderedReduced;
  });

  $("ul#orders").append(
       "<li><span class='orderInfo'>Order</span></li>");
     $(".orderInfo").last().click(function(){
       $("#show-info").show();
       $(".sides").text(sidesListed);
       $(".pizzaSauce").text(pizzaSauceListed);
       $(".pizzaToppings").text(pizzaToppingsListed);
       $(".beverages").text(beveragesListed);
       $(".desserts").text(dessertsListed);
       $(".zipcodeAddress").text(newOrder.zipcodeAddress);
       $(".streetAddress").text(newOrder.streetAddress);
       $(".cityAddress").text(newOrder.cityAddress);
       $(".total").text(newOrder.total);
     });
  //$('input:checkbox').removeAttr('checked');
  $("input#zipcodeAddress").val("");
  $("input#cityAddress").val("");
  $("input#streetAddress").val("");
});

});

1 个答案:

答案 0 :(得分:0)

更改

$(".total").text(newOrder.total);

$('.total').text(newOrder.total());

total是一种方法。您只是将其用作属性。