为什么我的JavaScript函数没有调用另一个函数?

时间:2020-11-05 22:04:52

标签: javascript

在html中,我调用了javascript函数setMode(int),该函数可以按预期工作。此函数完成参数输入,应再次调用其他javascript函数mode(String)。问题是,没有调用mode(String)。

function setMode(mode) {
  mode("?set=" + String(mode));
}

function mode(parameter) {
  parameter = (typeof parameter === 'undefined') ? '' : parameter;
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      switch (parseInt(this.responseText)) {
        case 0:
          document.getElementById("radio-auto2").checked = true;
          break;
        case 1:
          document.getElementById("radio-open2").checked = true;
          break;
        case 2:
          document.getElementById("radio-close2").checked = true;
          break;
        default:
      }
    }
  };
  xhttp.open("GET", "mode" + String(parameter), true);
  xhttp.send();
}

2 个答案:

答案 0 :(得分:2)

函数参数with open('path/to/file.txt', 'r') as f: #this opens the file in read mode lines = [line for line in f.readlines() if line.strip() != ""] #this puts all the lines of the file into a nice list, but removes every line that is empty. with open('path/to/file.txt', 'w') as f: #this just clears the contents pass for line in lines: #this goes through the lines list with open('path/to/file.txt', 'a') as f: #this opens the file append mode f.write(f'{line.strip()}\n') #this strips the line, and adds it at the end of the file and creates a new line 隐藏了作用域较高的函数mode。 JavaScript不会保留名称的字典,而不会根据名称的使用来推断要使用的名称,因此无法知道您编写的第一个mode(parameter)应该与名称不同。第二mode。您必须自己做出区分。

最简单的解决方案是更改参数名称或函数名称,以使一个不会隐藏另一个。我认为mode是一个函数的可怕名称,因为它没有描述该函数的功能。尝试将其重命名为mode或其他名称。

答案 1 :(得分:1)

尝试更改变量的名称,以使函数和变量不会被称为同一事物