从 Windows 命令提示符运行 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41777585/
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
Running JavaScript from the windows command prompt
提问by Wais Kamal
I wrote the following JavaScipt code which converts a binary number into a decimal number:
我编写了以下将二进制数转换为十进制数的 JavaScipt 代码:
(function bin_dec(num) {
var x = num;
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
return result;
})()
I want to be able to run this code from the command-line. The filename is converter.jsand I am running the command prompt window in the same directory as the file. I am trying to run this code using 01001100as the function argument. Here are my attempts:
我希望能够从命令行运行此代码。文件名是converter.js,我在与文件相同的目录中运行命令提示符窗口。我正在尝试使用01001100作为函数参数运行此代码。这是我的尝试:
$ converter.js 01001100
and
和
$ converter.js -01001100
and
和
$ converter.js bin_dec(01001100)
But unfortunately, neither of these works. Can somebody please point out my mistake? Thanks in advance.
但不幸的是,这些都不起作用。有人可以指出我的错误吗?提前致谢。
回答by GOTO 0
1) Install Node.jsif you haven't done so yet.
1)如果您还没有安装Node.js,请安装。
2) Change your converter.jsfile like this:
2)converter.js像这样更改您的文件:
function bin_dec(num) {
return parseInt(num, 2);
}
console.log(bin_dec(process.argv[2]));
3) Open a new command prompt in the folder where your script is and run
3)在脚本所在的文件夹中打开一个新的命令提示符并运行
$ node converter.js 01001100
回答by Ibrahim
In nodejs, assuming that you already have it installed since you are trying to run javascript from cmd, you will have to refer to the process.argvarray in order to get the arguments passed in the command line.
在 nodejs 中,假设您已经安装了它,因为您尝试从 cmd 运行 javascript,您将不得不引用process.argv数组以获取在命令行中传递的参数。
So your code will need to look like this:
因此,您的代码需要如下所示:
(function bin_dec(num) {
var x = num;
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
return result;
})(process.argv[2])
Notice process.argv[2]that is passed to the function. That will make the first argument available as the numvariable inside your function.
注意process.argv[2]传递给函数。这将使第一个参数可用作num函数内的变量。
You may also add a console.logsomewhere if you want to have messages displayed on the screen, since the return statement will not print messages.
console.log如果您想在屏幕上显示消息,您也可以在某处添加一个,因为 return 语句不会打印消息。
回答by Bassie
Assuming you are running on Windows, You can call it like this:
假设你在 Windows 上运行,你可以这样称呼它:
(function bin_dec() {
var x = WScript.arguments(0);
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
return result;
})()
Where all parameters passed into the function are stored in WScript.arguments.
传递给函数的所有参数都存储在WScript.arguments.
However, this will not output the returnvalue to the command prompt, so you may want to test it out with this .jsfile:
但是,这不会将return值输出到命令提示符,因此您可能需要使用此.js文件对其进行测试:
(function ShowAlert() {
var x = WScript.arguments(0);
WScript.ECho(x);
})()
See these links for more details:
有关更多详细信息,请参阅这些链接:
回答by Nikhilesh Shivarathri
You'll have to know that, few basic functions like consoleor returnstatements will not work in CLI.
您必须知道,很少有像consoleorreturn语句这样的基本函数在 CLI 中不起作用。
If you're using windows, then use 'WScript.echo'which is similar to console.logand while executing the file make sure you do it this way like
如果您使用的是 windows,则使用'WScript.echo'which 类似于console.log并在执行文件时确保您这样做
Cscript.exe yourpath input_paramsfor example Cscript.exe converter.js 01001100
Cscript.exe yourpath input_params例如 Cscript.exe converter.js 01001100
So your code should be
所以你的代码应该是
(function bin_dec() {
var x = WScript.arguments(0);
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
WScript.echo(result);
})();
and to run it should be
并运行它应该是
Cscript.exe converter.js 01001100
hope this helps you! For more information on CLI methods visit the following link
希望这对你有帮助!有关 CLI 方法的更多信息,请访问以下链接
https://msdn.microsoft.com/en-us/library/2795740w(v=vs.84).aspx
https://msdn.microsoft.com/en-us/library/2795740w(v=vs.84).aspx

