尝试返回true或false以确定是否定义了数组javascript

时间:2016-02-15 15:44:06

标签: javascript jquery

我正在尝试返回$ ./get_min < input_file > output_file true以确定是否在JavaScript中定义了数组,如下所示: CodePen link

false

我想在//display function object to display stuff display(value) { //reset display marker to reset the display var resetDisplay = false; //check the arugments if number set it to display or if an operation highlight the function if (isNaN(value[0]) && value[0] != ".") { //highlight selected operation $(id[value]).css("padding", "5px"); //if clear pressed set display to zero if (value == "=") { $("#display").text("0"); } //set reset display to true resetDisplay = true; } else { //display the value by concatenating it unless reset display is set to true after operation pressed if (resetDisplay = true) { $("#display").text(value); //reset the display to not true to continue to concatenate resetDisplay = false; } else { //continue to concatenate display $("display").text($("#display").text().concat(value)); console.log($("#display").text().concat(value)); } } //helper object that correlates with #id for each button var id = { "+": "#add", "-": "#subtract", "*": "#multiply", "/": "#divide", "=": "#equals", "1": "#one", "2": "#two", "3": "#three", "4": "#four", "5": "#five", "6": "#six", "7": "#seven", "8": "#eight", "9": "#nine", "0": "#zero", ".": "#point" } }

找出这个错误
  

无法读取&#39; - &#39;未定义。

我正在尝试从按钮发送操作。此函数在计算器对象中发生。

1 个答案:

答案 0 :(得分:0)

您的主要问题在于您检查resetDisplay变量的方式。通过使用=运算符,您实际上是将true分配给resetDisplay,而不是检查true

您可以非常轻松地通过身份(严格相等)true检查===,或者只使用if(variableName)执行以下操作,无论如何都应检查true

if (resetDisplay) {
    $("#display").text(value);
    //reset the display to not true to continue to concatenate
    resetDisplay = false;
}

如果您$(id[value])需要确保id在之前声明为,则使用它。所以请确保通过声明它来解决这个问题:

//display function object to display stuff
display(value) {
  //reset display marker to reset the display
  var resetDisplay = false,
      id = { //helper object that correlates with #id for each button
        "+": "#add",
        "-": "#subtract",
        "*": "#multiply",
        "/": "#divide",
        "=": "#equals",
        "1": "#one",
        "2": "#two",
        "3": "#three",
        "4": "#four",
        "5": "#five",
        "6": "#six",
        "7": "#seven",
        "8": "#eight",
        "9": "#nine",
        "0": "#zero",
        ".": "#point"
      };

  //check the arugments if number set it to display or if an operation highlight the function 
  if (isNaN(value[0]) && value[0] != ".") {
    //highlight selected operation
    $(id[value]).css("padding", "5px");
    //if clear pressed set display to zero
    if (value == "=") {
      $("#display").text("0");
    }

    //set reset display to true
    resetDisplay = true;

  } else {
    //display the value by concatenating it unless reset display is set to true after operation pressed
    if (resetDisplay) {
      $("#display").text(value);
      //reset the display to not true to continue to concatenate
      resetDisplay = false;
    } else {
      //continue to concatenate display
      $("display").text($("#display").text().concat(value));
      console.log($("#display").text().concat(value));
    }
  }
}