如何让 JavaScript 控制台用户输入工作(node.js)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28734674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get JavaScript console user input working (node.js)?
提问by 28Ostriches
This is some homework I have. I want it to read user input and then create two objects, giving them values based on the user input. Here is my code, beginning at the top of the JS file:
这是我的一些功课。我希望它读取用户输入,然后创建两个对象,根据用户输入为它们赋值。这是我的代码,从 JS 文件的顶部开始:
console.log("Type 'help' for commands");
console.log("Square numbers:");
console.log("Enter player 1 name");
var player1 = new Player(readLine());
var player2 = new Player(readLine());
console.log("logging name after making players" + player1.name);
function readLine() {
var inputText;
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter player name? ", function(answer) {
inputText = answer;
console.log("Player name:", answer);
rl.pause();
});
return inputText;
}
Here is the output I get: http://oi60.tinypic.com/9tn7nn.jpg
这是我得到的输出:http: //oi60.tinypic.com/9tn7nn.jpg
I'm used to C# and Java and in those languages, readline() would wait for the input and then create the object with whatever I inputted, before moving onto the next line. Not in JS though! It just carries on, calling readline() twice before I've typed anything in (therefore printing "prompt>" twice) and then logging player 1's name (before typing it in, hence undefined). Then it waits for me to type something in and once I type something, it sets both players to the one name and then ends the program.
我习惯了 C# 和 Java,在这些语言中, readline() 会等待输入,然后使用我输入的任何内容创建对象,然后再转到下一行。虽然不是在 JS 中!它只是继续,在我输入任何内容之前调用 readline() 两次(因此打印“prompt>”两次)然后记录玩家 1 的名字(在输入之前,因此未定义)。然后它等待我输入一些东西,一旦我输入一些东西,它就会将两个玩家设置为同一个名字,然后结束程序。
Is it possible to do what I am trying to get it to do? Is there another way I can get input to work without it skipping past it and running the rest of the code?
是否有可能做我想让它做的事情?有没有另一种方法可以让输入工作而不跳过它并运行其余代码?
Thanks in advance!
提前致谢!
回答by loganfsmyth
You have made several incorrect assumptions about JavaScript. The primary issue being that rl.question(...)
shows the prompt to the user, but it can do nothing to block the JS thread itself, so your readline()
function will immediately exit with undefined
because function(answer)
has not run yet because the user has not typed anything yet. A simple way to structure your code would be:
您对 JavaScript 做出了几个错误的假设。主要问题是rl.question(...)
向用户显示提示,但它无法阻止 JS 线程本身,因此您的readline()
函数将立即退出,undefined
因为function(answer)
尚未运行,因为用户尚未输入任何内容。构建代码的一种简单方法是:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.write("Type 'help' for commands\n");
rl.write("Square numbers:\n");
function createPayer(number, callback){
rl.question("Enter player " + number + " name? ", function(answer) {
var player = new Player(answer);
// Call the callback function once the player is created.
callback(player);
});
}
createPayer(1, function(player1){
createPayer(2, function(player2){
console.log("logging name after making players" + player1.name);
console.log("logging name after making players" + player2.name);
// Then call player logic you have from in here.
});
});
The key being that there is no explicit 'wait' anywhere in here. JS code is primarily event and callback based, so instead of waiting for the user to type something, you queue up a function to be called once they have entered something.
关键是这里没有明确的“等待”。JS 代码主要基于事件和回调,因此不是等待用户输入内容,而是在用户输入内容后将要调用的函数排入队列。
回答by cagcak
Just for a remainder:
仅用于余数:
the readline
module for nodejs has renamed as linebyline
.
readline
nodejs的模块已重命名为linebyline
.
Go for more info for linebyline.
And there is also a beautiful node module for console inputs for node js, called prompt
.
还有一个漂亮的节点模块,用于节点 js 的控制台输入,称为prompt
.