Javascript 程序中未定义 confirm() 函数
confirm() function not defined in Javascript program
我是 Javascript 的新手,如果我的查询太琐碎,请原谅我。我正在使用函数 "confirm()" 编写一个简单的程序。这是:
if (confirm("Are you xyz?"))
{
console.log("Hello xyz, how are you?");
} else {
console.log("Then what is your name?");
}
但是在 运行 上我收到错误:确认未定义。我是否需要安装或调用包来定义此 confirm() 函数?
浏览器 javascript 和 node.js 之间有一个非常重要的区别。浏览器有一个 confirm
函数,而 node 没有。如果你想做类似的事情,你可以使用 readline 模块。
const readline = require("readline");
const interface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
interface.question("Are you xyz? (y/n) ", function(ans) {
if (ans == "y" || ans == "yes") {
console.log("Hello there xyz.");
} else {
console.log("So what is your name?");
}
// pause the interface so the program can exit
interface.pause();
});
我是 Javascript 的新手,如果我的查询太琐碎,请原谅我。我正在使用函数 "confirm()" 编写一个简单的程序。这是:
if (confirm("Are you xyz?"))
{
console.log("Hello xyz, how are you?");
} else {
console.log("Then what is your name?");
}
但是在 运行 上我收到错误:确认未定义。我是否需要安装或调用包来定义此 confirm() 函数?
浏览器 javascript 和 node.js 之间有一个非常重要的区别。浏览器有一个 confirm
函数,而 node 没有。如果你想做类似的事情,你可以使用 readline 模块。
const readline = require("readline");
const interface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
interface.question("Are you xyz? (y/n) ", function(ans) {
if (ans == "y" || ans == "yes") {
console.log("Hello there xyz.");
} else {
console.log("So what is your name?");
}
// pause the interface so the program can exit
interface.pause();
});