如何在 SpiderMonkey JavaScript 中获取控制台输入?

发布于 2024-09-07 10:28:54 字数 287 浏览 6 评论 0原文

我目前正在使用 Spidermonkey 来运行我的 JavaScript 代码。我想知道是否有一个函数可以从控制台获取输入,类似于 Python 的做法:

var = raw_input()  

或者在 C++ 中:

std::cin >> var;

我环顾四周,到目前为止我发现的是如何使用 prompt()confirm() 函数。

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:

var = raw_input()  

Or in 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放赐 2024-09-14 10:28:54

很好的老readline();

请参阅 MDN(存档)。

Good old readline();.

See MDN (archive).

焚却相思 2024-09-14 10:28:54

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

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

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

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

是伱的 2024-09-14 10:28:54

正如您提到的,提示 适用于一直到 IE 的浏览器:

var answer = prompt('question', 'defaultAnswer');

IE 中的提示

对于 Node.js > v7.6,您可以使用 console-read-write,它是低级 readline 模块的包装器:

const io = require('console-read-write');

async function main() {
  // Simple readline scenario
  io.write('I will echo whatever you write!');
  io.write(await io.read());

  // Simple question scenario
  io.write(`hello ${await io.ask('Who are you?')}!`);

  // Since you are not blocking the IO, you can go wild with while loops!
  let saidHi = false;
  while (!saidHi) {
    io.write('Say hi or I will repeat...');
    saidHi = await io.read() === 'hi';
  }

  io.write('Thanks! Now you may leave.');
}

main();
// I will echo whatever you write!
// > ok
// ok
// Who are you? someone
// hello someone!
// Say hi or I will repeat...
// > no
// Say hi or I will repeat...
// > ok
// Say hi or I will repeat...
// > hi
// Thanks! Now you may leave.

披露我是 Console-read-write

For SpiderMonkey 的作者和维护者,简单的 readline@MooGoo@Zaz

As you mentioned, prompt works for browsers all the way back to IE:

var answer = prompt('question', 'defaultAnswer');

prompt in IE

For Node.js > v7.6, you can use console-read-write, which is a wrapper around the low-level readline module:

const io = require('console-read-write');

async function main() {
  // Simple readline scenario
  io.write('I will echo whatever you write!');
  io.write(await io.read());

  // Simple question scenario
  io.write(`hello ${await io.ask('Who are you?')}!`);

  // Since you are not blocking the IO, you can go wild with while loops!
  let saidHi = false;
  while (!saidHi) {
    io.write('Say hi or I will repeat...');
    saidHi = await io.read() === 'hi';
  }

  io.write('Thanks! Now you may leave.');
}

main();
// I will echo whatever you write!
// > ok
// ok
// Who are you? someone
// hello someone!
// Say hi or I will repeat...
// > no
// Say hi or I will repeat...
// > ok
// Say hi or I will repeat...
// > hi
// Thanks! Now you may leave.

Disclosure I'm author and maintainer of console-read-write

For SpiderMonkey, simple readline as suggested by @MooGoo and @Zaz.

乄_柒ぐ汐 2024-09-14 10:28:54

您可以尝试类似 process.argv 的内容,也就是说,如果您使用 node.js 来运行程序。

console.log(process.argv) =>;将打印一个数组,其中包含

[                                                                                                                                                                                          
  '/usr/bin/node',                                                                                                                                                                         
  '/home/user/path/filename.js',                                                                                                                                            
  'your_input'                                                                                                                                                                                   
]

您通过数组索引获取用户提供的输入,即 console.log(process.argv[3]) 这应该为您提供可以存储的输入。

示例:

var somevariable = process.argv[3]; // input one
var somevariable2 = process.argv[4]; // input two

console.log(somevariable);
console.log(somevariable2);

如果您正在构建命令行程序,则 npm 包 yargs 真的很有帮助。

You can try something like process.argv, that is if you are using node.js to run the program.

console.log(process.argv) => Would print an array containing

[                                                                                                                                                                                          
  '/usr/bin/node',                                                                                                                                                                         
  '/home/user/path/filename.js',                                                                                                                                            
  'your_input'                                                                                                                                                                                   
]

You get the user provided input via array index, i.e., console.log(process.argv[3]) This should provide you with the input which you can store.

Example:

var somevariable = process.argv[3]; // input one
var somevariable2 = process.argv[4]; // input two

console.log(somevariable);
console.log(somevariable2);

If you are building a command-line program then the npm package yargs would be really helpful.

梦里人 2024-09-14 10:28:54

Node.js 内置了 readline 模块。

一个例子:

const readline = require('readline')
const rl = readline.createInterface({
   input: process.stdin,
   output: process.stdout,
});
rl.question(`Are you sure? (yes/no): `, async answer => {
   if (answer.toLocaleLowerCase() === 'yes') {
     console.log('processing...');
   }
   else {
     console.log('aborting...');
   }
});

Node.js has built-in readline module.

one example:

const readline = require('readline')
const rl = readline.createInterface({
   input: process.stdin,
   output: process.stdout,
});
rl.question(`Are you sure? (yes/no): `, async answer => {
   if (answer.toLocaleLowerCase() === 'yes') {
     console.log('processing...');
   }
   else {
     console.log('aborting...');
   }
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文