如何编写一个简单的 JScript 输入/输出程序?
我计划明天使用 JavaScript 参加信息学竞赛 (BIO)。然而,我不能依赖考官拥有一个带有像样的 JavaScript 引擎的浏览器,所以我希望使用 Microsoft 的 JScript。
然而,坦率地说,该文档是垃圾。有人可以发布一些示例代码来读取一行文本,对其调用 foo(string) 并将输出回显到命令行吗?
同样,我如何实际运行它? wscript.exe PATH_TO_JS_FILE
可以解决问题吗?
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.
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?
Similarly, how do I actually run it? Will wscript.exe PATH_TO_JS_FILE
do the trick?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用命令行,我将使用
CSCRIPT.EXE
执行脚本。即:
CSCRIPT.EXE myscript.js
这是因为
WSCRIPT
中的WScript.Echo
将创建一个对话框,并从CSCRIPT
向控制台输出一行。在命令窗口 (CMD) 中运行此命令。从控制台读取一行到变量中:
其中
StdIn
是 TextStream 对象。还有一个StdOut
可以用来代替WScript.Echo()
...将
foo(x)
的输出写入控制台:(必须在CSCRIPT
下运行)您可以使用
WScript
对象来确定您在哪个引擎下运行,有一个问题/答案(VBScript,但使用JScript 下的相同对象)此处。If you're using the command-line, I'd execute the script using
CSCRIPT.EXE
.ie:
CSCRIPT.EXE myscript.js
This is because
WScript.Echo
fromWSCRIPT
will create a dialog box and fromCSCRIPT
outputs a line to the console. Run this in a command window (CMD).Reading a line from console into variable:
Where
StdIn
is a TextStream object. There is also anStdOut
which can be used in place ofWScript.Echo()
...Writing the output of
foo(x)
to console: (must run underCSCRIPT
)You can use the
WScript
object 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.StdIn
和WScript.StdOut
对象/属性:并使用
运行它>cscript.exe
。但如果你想让它成为一个 GUI 程序,考虑到 JScript 没有像VBScript
那样的原生InputBox
函数,那就有点困难了。但是,正如此处所述,我们可以使用Windows 脚本宿主 (WSH)。创建.wsf
文件:并使用
cscript.exe
或wscript.exe
运行它。或者,您也可以使用 HTML 应用程序 (HTA) 来创建更复杂的 GUI。If you want to be sure your program only runs in the command line, you may use the
WScript.StdIn
andWScript.StdOut
objects/properties:and run it with
cscript.exe
. But if you want it to be a GUI program, it is a bit more difficult considering that JScript doesn't have a nativeInputBox
function likeVBScript
. However, as described here we may use Windows Script Host (WSH). Create a.wsf
file:and run it with either
cscript.exe
orwscript.exe
. Alternatively, you could also use HTML Application (HTA) to create more elaborate GUIs.