JS中的Prototype问题

时间:2016-08-31 02:59:51

标签: javascript object constructor prototype

对于我的课,我需要创建一个构造函数,对象和原型。不幸的是,我不能让原型工作,并想知道是否有人可以帮助我了解如何制作原型来正确计算我的披萨对象的价格。在我必须添加一个对象和原型之前,它工作正常,但为了满足分配指南,我被要求返回并制作原型,这让我非常困惑。任何帮助将不胜感激。

//business logic
function Pizza(size, sauce, cheese, meat1, meat2, veggie1, veggie2) {
this.size = size;
this.sauce = sauce;
this.cheese = cheese;
this.meat1 = meat1;
this.meat2 = meat2;
this.veggie1 = veggie1;
this.veggie2 = veggie2;
}
Pizza.prototype.pizzaPrice = function() {
return pizzaPrice = inputtedSize + inputtedSauce + inputtedCheese + inputtedMeatOne + inputtedMeattwo + inputtedVeggieOne + inputtedVeggieTwo + 0;
}

//user interface logic
$(document).ready(function(){
$("form").submit(function(event) {
event.preventDefault();
$("form").fadeOut();
var inputtedSize = parseInt($("#size").val());
var inputtedSauce = parseInt($("#sauce").val());
var inputtedCheese = parseInt($("#cheese").val());
var inputtedMeatOne = parseInt($("#meat1").val());
var inputtedMeattwo = parseInt($("#meat2").val());
var inputtedVeggieOne = parseInt($("#veggie1").val());
var inputtedVeggieTwo = parseInt($("#veggie2").val());
var sizeChoice = $( "#size option:selected" ).text();
var sauceChoice = $( "#sauce option:selected" ).text();
var cheeseChoice = $( "#cheese option:selected" ).text();
var meatChoiceOne = $( "#meat1 option:selected" ).text();
var meatChoiceTwo = $( "#meat2 option:selected" ).text();
var veggieChoiceOne = $( "#veggie1 option:selected" ).text();
var veggieChoiceTwo = $( "#veggie2 option:selected" ).text();
var pizza = Pizza();
var newPizza = (inputtedSize + inputtedSauce + inputtedCheese + inputtedMeatOne + inputtedMeattwo + inputtedVeggieOne + inputtedVeggieTwo);
Pizza.pizzaPrice(newPizza);
$("#total").fadeIn();
$(".total").text(" " + "$" + newPizza);
$(".size").text(" " + sizeChoice);
$(".sauce").text(" " + sauceChoice);
$(".cheese").text(" " + cheeseChoice);
$(".meat1").text(" " + meatChoiceOne);
$(".meat2").text(" " + meatChoiceTwo);
$(".veggie1").text(" " + veggieChoiceOne);
$(".veggie2").text(" " + veggieChoiceTwo);
 });
});

2 个答案:

答案 0 :(得分:0)

template<class... T>
void f(void(*...t)(T)) {
  std::cout << "Ptr to functions" << std::endl;
}

template<class... T>
void g(void(&...t)(T)) {
  std::cout << "Ref to functions" << std::endl;
}

template <class... T>
void h(void(...t)(T)) {
  std::cout << "Mmmm... potential of confusion" << std::endl;
}

void s1(int) {

}

void s2(long) {

}


#include <type_traits>
int main() {
  // this compiles Ok. The compiler knows
  //    pointer to functions will be passed
  //    and can infer their correspondent T
  f(s1,s2);

  // this compiles Ok. The compiler knows that
  //   reference to functions will be passed
  //   and can infer their correspondent T
  g(s1,s2);

     h(s1,s2);
  // ^^^^^^^^^ this will cause a compilation error telling:
  //   template argument deduction/substitution failed:
  //   mismatched types ‘void(T)’ and ‘void (*)(int)’
  // The compiler can't decide how the function-type parameters are passed
  //  by ptr, by ref, one by ref other by ptr? As such
  //  cannot deduce their correspondent T in the argpack 


  // however, this will compile OK!!!
  // The s1/s2 correspondent T-es in argpack are clearly specified, 
  // not a problem.
  h<int,long>(s1,s2);
  return 0;
}

我是新手,但试试这个。

答案 1 :(得分:0)

首先,您错误地实例化了对象。您必须使用自解释new运算符为新对象实例添加前缀。

其次,您使用this引用实例值。在原型函数中,您可以通过在变量前加this来访问实例变量。在构造函数中执行this.size = size将产生您在调用this.size时随时传递给构造函数的值。因此,您的函数应该引用那些值,而不是您从中设置输入值的变量。

以下是您的代码的外观:

function Pizza(size, sauce, cheese, meat1, meat2, veggie1, veggie2) {
  this.size = size;
  this.sauce = sauce;
  this.cheese = cheese;
  this.meat1 = meat1;
  this.meat2 = meat2;
  this.veggie1 = veggie1;
  this.veggie2 = veggie2;
}
Pizza.prototype.price = function() {
  return this.size + 
    this.sauce + 
    this.cheese + 
    this.meat1 + 
    this.meat2 + 
    this.veggie1 + 
    this.veggie2 + 0;
}

var pizza = new Pizza(1, 1, 2, 1, 2, 3, 1);

console.log(pizza) // Pizza instance
console.log(pizza.price()) // returns computed instance values
console.log(pizza.sauce) // access instance value

在您的情况下,您将使用上面的示例:

$(document).ready(function() {
  $("form").submit(function(event) {
    event.preventDefault();
    $("form").fadeOut();
    var inputtedSize = parseInt($("#size").val());
    var inputtedSauce = parseInt($("#sauce").val());
    var inputtedCheese = parseInt($("#cheese").val());
    var inputtedMeatOne = parseInt($("#meat1").val());
    var inputtedMeattwo = parseInt($("#meat2").val());
    var inputtedVeggieOne = parseInt($("#veggie1").val());
    var inputtedVeggieTwo = parseInt($("#veggie2").val());
    var sizeChoice = $("#size option:selected").text();
    var sauceChoice = $("#sauce option:selected").text();
    var cheeseChoice = $("#cheese option:selected").text();
    var meatChoiceOne = $("#meat1 option:selected").text();
    var meatChoiceTwo = $("#meat2 option:selected").text();
    var veggieChoiceOne = $("#veggie1 option:selected").text();
    var veggieChoiceTwo = $("#veggie2 option:selected").text();

    var pizza = new Pizza(inputtedSize, inputtedSauce, inputtedCheese, inputtedMeatOne, inputtedMeattwo, inputtedVeggieOne, inputtedVeggieTwo);

    $("#total").fadeIn();
    $(".total").text(" " + "$" + pizza.price());
    $(".size").text(" " + pizza.size);
    $(".sauce").text(" " + pizza.sauce);
    $(".cheese").text(" " + pizza.cheese);
    $(".meat1").text(" " + pizza.meat1);
    $(".meat2").text(" " + pizza.meat2);
    $(".veggie1").text(" " + pizza.veggie1);
    $(".veggie2").text(" " + pizza.veggie1);
  });
});