如何在javascript中获得控制台输入?

时间:2022-03-14 20:43:52

I'm currently using spidermonkey to run my JavaScript code. I'm wondering if there's a function to get input from the console similar to how Python does this:

我目前正在使用spidermonkey来运行我的JavaScript代码。我想知道是否有一个从控制台获取输入的函数,类似于Python的工作方式:

var = raw_input()  

Or in C++:

或者在C ++中:

std::cin >> var;

I've looked around and all I've found so far is how to get input from the browser using the prompt() and confirm() functions.

我环顾四周,到目前为止我发现的是如何使用prompt()和confirm()函数从浏览器获取输入。

2 个解决方案

#1


28  

Good old readline();

好旧的readline();

See MDN docs: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell#readline.28.29

请参阅MDN文档:https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell#readline.28.29

#2


10  

In plain JavaScript, simply use response = readline() after printing a prompt.

在纯JavaScript中,只需在打印提示后使用response = readline()。

In Node.js, you'll need to use the readline module: const readline = require('readline')

在Node.js中,您需要使用readline模块:const readline = require('readline')

#1


28  

Good old readline();

好旧的readline();

See MDN docs: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell#readline.28.29

请参阅MDN文档:https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell#readline.28.29

#2


10  

In plain JavaScript, simply use response = readline() after printing a prompt.

在纯JavaScript中,只需在打印提示后使用response = readline()。

In Node.js, you'll need to use the readline module: const readline = require('readline')

在Node.js中,您需要使用readline模块:const readline = require('readline')