javascript 如何编写一个简单的 JScript 输入/输出程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4441003/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:27:01  来源:igfitidea点击:

How can I write a simple JScript input/output program?

javascriptwshjscript

提问by Eric

I'm planning on using JavaScript to enter an informatics competition (BIO) tomorrow. However, I can't rely on the examiner having a browser with a decent JavaScript engine, so I was hoping to use Microsoft's JScript instead.

我计划明天使用 JavaScript 参加信息学竞赛 (BIO)。但是,我不能指望考官拥有带有不错 JavaScript 引擎的浏览器,所以我希望改用 Microsoft 的 JScript。

However, the documentation is, quite frankly, crap. Can someone post some example code that reads in a line of text, calls foo(string)on it, and echos the output to the command line?

然而,坦率地说,文档是垃圾。有人可以发布一些示例代码,读取一行文本,调用foo(string)它,并将输出回显到命令行吗?

Similarly, how do I actually run it? Will wscript.exe PATH_TO_JS_FILEdo the trick?

同样,我如何实际运行它?会wscript.exe PATH_TO_JS_FILE做伎俩吗?

回答by aikeru

If you're using the command-line, I'd execute the script using CSCRIPT.EXE. ie: CSCRIPT.EXE myscript.jsThis is because WScript.Echofrom WSCRIPTwill create a dialog box and from CSCRIPToutputs a line to the console. Run this in a command window (CMD).

如果您使用命令行,我会使用CSCRIPT.EXE. 即:CSCRIPT.EXE myscript.js这是因为WScript.EchofromWSCRIPT将创建一个对话框,并且 fromCSCRIPT将一行输出到控制台。在命令窗口 (CMD) 中运行它。

Reading a line from console into variable:

从控制台读取一行到变量中:

var x = WScript.StdIn.ReadLine();

Where StdInis a TextStream object. There is also an StdOutwhich can be used in place of WScript.Echo()...

哪里StdIn是一个TextStream对象。还有一个StdOut可以用来代替WScript.Echo()...

Writing the output of foo(x)to console: (must run under CSCRIPT)

将输出写入foo(x)控制台:(必须在 下运行CSCRIPT

WScript.Echo(foo(x));

You can use the WScriptobject to determine which engine you are running under, there's a question/answer for that (VBScript, but uses the same objects under JScript) here.

您可以使用WScript对象来确定你是下运行,其发动机,有一个问题/答案为(VBScript中,但在JScript中使用相同的对象)这里