如何在命令行中运行/调试javascript [英] how to run / debug javascript in command line

查看:478
本文介绍了如何在命令行中运行/调试javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道(实际上我知道有一种方式来运行js在命令行,因为当我观看google I / 0演示,他们使用命令像d8,也许它是V8引擎的一部分)是有任何方式来运行/调试我的javascripts在命令行?例如 d8 myJsfile.js --prof 等。

I'm wondering (actually I know there definitely is such a way to run js in command line, because when I watch google I/0 presentations, they use a command like d8, maybe it's part of V8 engine) is there any way to run/debug my javascripts in command line? such as d8 myJsfile.js --prof etc.?

有没有人知道这个?

推荐答案

是的,D8是一个命令行工具,允许您运行和调试Javascript脚本。

Yes, D8 is a command line tool that allows you to run and debug Javascript scripts. It comes with V8.

在您的机器上获得V8并不难。按照以下说明操作:
https://developers.google.com/v8/build

Getting V8 on your machine is not hard. Follow these instructions: https://developers.google.com/v8/build

(有GYP的部分看起来有点乱,但我在Linux盒子上很容易找到它。)

(The part with GYP looks a little messy but I got through it easily on a Linux box.)

使用V8安装,您现在有了d8,它允许您从命令行运行javascript。

With V8 installed, you now have d8, which allows you to run javascript from the command line.

$ d8 myscript.js



使用D8



c $ c> d8 --help 用于分析选项。

如果您要使用调试启动d8,请使用以下选项: -debugger 。在代码中遇到语句 debugger; 时,或者在d8中键入 debugger; 贝壳。 (你通过启动d8与 - shell 。)你知道你正在调试shell提示从 d8> dbg> 。要在 dbg> 提示符下获得有关调试器类型 help 的帮助。

If you want to debug start d8 with --debugger. The script will break when it encounters the statement debugger; in your code, or when you type debugger; in the d8 shell. (You get the shell by starting d8 with --shell.) You know you're debugging when the shell prompt goes from d8> to dbg>. To get help with the debugger type help at the dbg> prompt.

d8调试器是一个古老的命令行调试器,如果你习惯于GUI调试器,将很难在大型复杂脚本中使用。

The d8 debugger is an old school command line debugger that will be painful to use on large complex scripts if you are used to GUI debuggers.

如果要使用GUI调试器,您可以使用Chrome DevTools for Java,其中包括Eclipse调试器:

If you want to use a GUI debugger you can use the Chrome DevTools for Java, which includes an Eclipse debugger:

https://code.google.com/p/chromedevtools/

要使其正常工作:

要调试在D8中运行的代码:

To debug code running in D8:


  • 使用您的脚本和--debugger_agent启动D8,如有必要,根据您的配置启动--debugger_port; D8的默认端口为5858。

  • 在Eclipse中启动您在上面创建的调试配置。脚本将显示在界面中

  • 在Eclipse UI中设置断点并在其中。 (您不需要在代码中使用调试程序语句。)

  • start D8 with your script and with --debugger_agent, and --debugger_port if necessary based on your config; D8's default port is 5858.
  • in Eclipse launch the debug configuration you created above. The scripts will show up in the interface
  • set breakpoints in the Eclipse UI and have at it. (You do not need debugger; statements in your code.)

注! D8只侦听本地主机上的调试器。所以如果你在一个单独的机器,那么这将不工作。没有标志,它将使其监听另一个IP。请参阅:
https://code.google.com/p / v8 / issues / detail?id = 1855

Note! D8 only listens for the debugger on the localhost. So if you are on a separate machine, then this won't work. There is no flag that will make it listen on another IP. See: https://code.google.com/p/v8/issues/detail?id=1855

请注意,D8中的Javascript环境与浏览器窗口中的JavaScript环境不同。有多种函数不能使用:

Note that the Javascript environment in D8 is not the same as the one in a browser window. There are a number of functions you can't use:


  • setTimeout及其表兄弟

  • XMLHttpRequest

  • 还有更多。检查ECMA规格以查看实际可以使用的内容。

记住没有 code>或文档。或DOM!

这篇关于如何在命令行中运行/调试javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆