变量返回undefined,无论它设置为什么

时间:2017-05-19 12:17:58

标签: javascript variables console.log

在此函数中,我初始化变量allowDial(),然后定义它。

我的值有些问题所以我在offHook()函数的末尾添加了一个console.log。我之前定义它。无论我将allowDial设置为什么,它都会返回undefined - 无论是数字,布尔值,字符串等。

为什么console.log返回undefined?如果我无法让console.log准确报告其值,我看不出如何使变量的实现工作。

以下是相关代码:

var allowDial;
var availableNumbers = ["0", "911", "1 (847) 765-1008" , "867-5309", "1 (212) 456-1414", "555-1212", "555-5555"];
function numberSuggestion() {
    var randomNumber = Math.floor(Math.random() * (availableNumbers.length));
    var suggestedNumber = availableNumbers[randomNumber];
    document.getElementById("suggestion").innerHTML = "How about dialing <strong id='suggestedTelephoneNumber'>" + suggestedNumber + "</strong>? Don't like this number? Click the button above again!";
    }
var dialTone;
function offHook() {
    document.getElementById("WE2500").style.display = "none";
    document.getElementById("dialPad").style.display = "block";
    dialTone = new Audio('dialTone.m4a');
    dialTone.play();
    allowDial === true;
    console.log(allowDial);
}

2 个答案:

答案 0 :(得分:1)

您没有为allowDial分配任何值。 ===用于比较,而非分配。要分配值,您应使用allowDial = true;

答案 1 :(得分:-3)

您不会为allowDial分配任何值。

allowDial = true;

示例:

var tmp;
testVar(tmp);       // "is null"
tmp = null;
testVar(tmp);       // "is null""is definitive null"

testVar()

function testVar(variable) {
  if(variable == null) { 
    console.log("is null");
  } 

  if(variable === null) { 
    console.log("is definitive null");
  }
}