【问题标题】:How do I get console input in spidermonkey JavaScript?如何在 spidermonkey JavaScript 中获取控制台输入?
【发布时间】:2011-03-08 10:31:29
【问题描述】:

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

var = raw_input()  

或者在 C++ 中:

std::cin >> var;

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

【问题讨论】:

    标签: javascript input console spidermonkey


    【解决方案1】:

    好老readline();。

    见MDN(存档)。

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • ` var x = readline(); ^ TypeError: readline is not a function ` 在文件顶部添加 require 后
    • 也遇到了同样的问题。
    • 这比以往任何时候都更奇怪。
    • readline 在节点中的工作方式不同。你需要这样的东西: const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout }) readline.question("what's your name?", name => { console.log (Hello ${name}); readline.close(); });
    • @kDar 我更喜欢 name => { readline.close();控制台.log(名称);就我而言,您的版本在键入时不显示输入。不管怎样,谢谢你的评论
    【解决方案3】:

    正如你所提到的,prompt 一直适用于浏览器,一直到 IE:

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

    对于 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.
    

    披露我是控制台读写的作者和维护者

    对于 SpiderMonkey,简单的 readline 由 @MooGoo 和 @Zaz 建议。

    【讨论】:

      【解决方案4】:

      您可以尝试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 将非常有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-08
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多