如何在类似控制台的环境中运行 JavaScript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8994011/
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
How do I run JavaScript in a console like environment?
提问by Hoa
I'm trying to follow the the examples here
我正在尝试按照此处的示例进行操作
http://eloquentjavascript.net/chapter2.html
http://eloquentjavascript.net/chapter2.html
and
和
print('blah');
Keeps trying to send to a physical printer when run in a browser. I just want console output.
在浏览器中运行时,不断尝试发送到物理打印机。我只想要控制台输出。
回答by Adam Rackis
If you want something simple, the Chrome console will let you run JavaScript on the fly. To print text you can use console.log('blah');
And if you want to print multiple values, you can just list them all out as arguments (no need to concatenate) console.log("a", "b", "c", 1, 2, 3);
如果您想要一些简单的东西,Chrome 控制台可以让您即时运行 JavaScript。打印您可以使用的文本console.log('blah');
如果您想打印多个值,您可以将它们全部作为参数列出(无需连接)console.log("a", "b", "c", 1, 2, 3);
You can bring it up with control-shift i
then select the console tab on the far right. And of course FireBug has a similar feature if you're more of a FireFox fan.
您可以打开它,control-shift i
然后选择最右侧的控制台选项卡。当然,如果您更喜欢 FireFox,FireBug 也有类似的功能。
回答by James Hill
Almost all modern browsers have developer tools, which includes the a console. Look for developer tools in your browser, then look for the console.
几乎所有现代浏览器都有开发人员工具,其中包括控制台。在浏览器中查找开发人员工具,然后查找控制台。
Once you've found it, you can enter expressions manually, or place console.log()
in the desired places in your code.
找到它后,您可以手动输入表达式,或将console.log()
其放置在代码中的所需位置。
Note:Once you get used to the console, it will replace the need for the ever so annoying alert(myVar)
.
注意:一旦您习惯了控制台,它将取代以往烦人的alert(myVar)
.
Additional Info
附加信息
Here's a decent overview/tutorial on using the console: https://web.archive.org/web/20150101222740/http://anemonesoft.com/os/cottage/doc/tutorial/console.html
这是有关使用控制台的不错的概述/教程:https: //web.archive.org/web/20150101222740/http: //anemonesoft.com/os/cottage/doc/tutorial/console.html
回答by octern
You can run javascript interactively from the command line. This blog postfrom ajaxian.com describes how to use the built-in javascript shells on Windows and Mac OS. When you're feeling especially lazy, you can also type commands into a browser-based javascript interpreter like this one.
您可以从命令行以交互方式运行 javascript。ajaxian.com 的这篇博文描述了如何在 Windows 和 Mac OS 上使用内置的 javascript shell。当您感到特别懒惰时,您还可以将命令输入到基于浏览器的 javascript 解释器中,就像这样。