【问题标题】:How to fix javascript reference error: prompt not defined [duplicate]如何修复javascript引用错误:提示未定义[重复]
【发布时间】:2019-08-31 18:18:43
【问题描述】:

我编写了用于简单重量转换的代码和一个要求用户输入的年度利润计算器程序。当我在 vscode 中运行程序时,它显示错误“ReferenceError:提示未定义。”

var weight_in_lbs = prompt("Please enter the weight in lbs you wish to convert:");
var pound_to_kg = (weight_in_lbs * .45359237);
console.log("The weight converted in kg is" + " " + pound_to_kg.toFixed(3))`
var annualSales = prompt("What is the projected total sales amount?");
var salesPrediction = (annualSales * .23);
console.log ("Your annual profit is " + salesPrediction.toFixed(2)) ;

【问题讨论】:

  • 您是否有机会在 Node 环境中运行它?
  • @Clarity OP 说它正在 vscode 中运行
  • @Pointy 听起来确实像是在节点环境中运行的。除非我错过了什么。
  • 请提供更多细节以继续提供建议

标签: javascript calculator prompt referenceerror


【解决方案1】:

prompt 不是 Node.js 运行时中的有效构造(假设是您通过 VSCode 运行的构造)。它只适用于基于浏览器的 JavaScript 引擎。

有些库提供了类似的功能,但您必须通过 NPM 安装并将它们导入到您的脚本中。

【讨论】:

    【解决方案2】:

    在使用 Electron 环境的 VS Code 中,不支持 prompt 函数,因为它是 UI 阻塞。请参阅此issue 了解更多信息。

    您需要在浏览器中运行您的示例,例如下面的 sn-p。

    var weight_in_lbs = prompt("Please enter the weight in lbs you wish to convert:");
    var pound_to_kg = (weight_in_lbs * .45359237);
    console.log("The weight converted in kg is" + " " + pound_to_kg.toFixed(3))
    var annualSales = prompt("What is the projected total sales amount?");
    var salesPrediction = (annualSales * .23);
    console.log("Your annual profit is " + salesPrediction.toFixed(2));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2020-02-03
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多