如何查找 Javascript 语法错误

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

How to find Javascript syntax errors

javascript

提问by Robert

I am writing a JavaScript section of code and am making huge amounts of silly mistakes in the syntax. But the only way to find what line the error is on is to start commenting out the section of code I just wrote and reload it into the browser to narrow down where my missing ');' is.

我正在编写一段 JavaScript 代码,并且在语法中犯了大量愚蠢的错误。但是找到错误所在行的唯一方法是开始注释掉我刚刚编写的代码部分并将其重新加载到浏览器中以缩小我丢失的');' 是。

How do you "compile" a JavaScript source to make sure it is syntactically correct so that I can debug it in the browser?

你如何“编译”一个 JavaScript 源代码以确保它在语法上是正确的,以便我可以在浏览器中调试它?

采纳答案by Carson Myers

Ctrl+ Shift+ Jin firefox, if you don't catch your mistakes until runtime. Of course firebug works better, but this is a quick way.

Ctrl+ Shift+J在 Firefox 中,如果您直到运行时才发现错误。当然,firebug 效果更好,但这是一种快速的方法。

回答by Tahir Akhtar

http://www.javascriptlint.com/

http://www.javascriptlint.com/

Here are some common mistakes that JavaScript Lint looks for:

以下是 JavaScript Lint 寻找的一些常见错误:

  • Missing semicolons at the end of a line.
  • Curly braces without an if, for, while, etc.
  • Code that is never run because of a return, throw, continue, or break.
  • Case statements in a switch that do not have a break statement.
  • Leading and trailing decimal points on a number.
  • A leading zero that turns a number into octal (base 8).
  • Comments within comments.
  • Ambiguity whether two adjacent lines are part of the same statement.
  • Statements that don't do anything.
  • 行尾缺少分号。
  • 没有 if、for、while 等的花括号。
  • 由于返回、抛出、继续或中断而永远不会运行的代码。
  • switch 中没有 break 语句的 case 语句。
  • 数字的前导和尾随小数点。
  • 将数字转换为八进制(基数为 8)的前导零。
  • 评论中的评论。
  • 两个相邻行是否属于同一语句的歧义。
  • 不做任何事情的语句。

回答by slebetman

Douglas Crockford's Jslint: http://www.jslint.com/

Douglas Crockford 的 Jslint:http: //www.jslint.com/

But be warned, it will hurt your feelings ;-)

但请注意,这会伤害您的感情;-)

回答by Gabriel

Chrome: Ctrl+ Shift+ J: this will bring up the JavaScript console. You can click the scripts tab to review page scripts.

Chrome:Ctrl+Shift+J: 这将打开 JavaScript 控制台。您可以单击脚本选项卡来查看页面脚本。

FireFox: Install firebug and run it to get a similar console to the above stated chrome utilities.

FireFox:安装 firebug 并运行它以获得与上述 chrome 实用程序类似的控制台。

IDE: You may wish to use netbeansor Eclipsewhich both offer syntax highlighting and code completion for JavaScript. These will indicate syntax errors at code time.

IDE:您可能希望使用netbeansEclipse,它们都为 JavaScript 提供语法突出显示和代码完成功能。这些将在代码时指示语法错误。

jslint/jshint: These tools offer code suggestions to improve your code quality and reduce the possibility of errors (logic) but will also break on syntax and other errors.

jslint/ jshint:这些工具提供代码建议以提高您的代码质量并减少错误(逻辑)的可能性,但也会破坏语法和其他错误。

回答by Dave Kiss

Try JSlint

试试JSlint

It's a great utility for debugging Javascript. If you're using the jQuery library, there is a plugin being developed and was just recently released, similarly called jQuery Lint

这是一个很好的调试 Javascript 的工具。如果您使用的是 jQuery 库,则有一个正在开发的插件最近刚刚发布,类似地称为jQuery Lint

回答by Edwin Yip

You can use the EsprimaJavaSript parser (the link is a online code editor ready to use).

您可以使用EsprimaJavaSript 解析器(该链接是一个可供使用的在线代码编辑器)。

I believe it's currently the best ready-to-use js parser in js, that's why I'm using it in my LIVEditorproject (code editor for the html/css/js with real-time preview).

我相信它目前是js 中最好的即用型js 解析器,这就是为什么我在我的LIVEditor项目(带有实时预览的 html/css/js 代码编辑器)中使用它的原因。

回答by vumaasha

I find JSHint, + its vim plugin are very useful. Light weight of vim and still be able to track the syntax errors of the javascript.

我发现 JSHint,+ 它的 vim 插件非常有用。轻量级的 vim 仍然能够跟踪 javascript 的语法错误。

https://github.com/walm/jshint.vim

https://github.com/walm/jshint.vim

回答by Tahir Akhtar

One idea would be to use Firebug console for interactively experimenting with you scripts and then moving only the tested parts into the your code.

一种想法是使用 Firebug 控制台对您的脚本进行交互试验,然后仅将经过测试的部分移动到您的代码中。

回答by Upperstage

The Eclipse IDE (with the JSEclipse plugin) identifies nearly all of our syntax problems.

Eclipse IDE(带有 JSEclipse 插件)确定了我们几乎所有的语法问题。

I suggest one of the better editors is IntelliJ, but it's not free.

我建议更好的编辑器之一是IntelliJ,但它不是免费的。

回答by Mic

If you are using Textmate (mac) you can install this bundle

如果你使用 Textmate (mac) 你可以安装这个包

It is mainly jslint, that will check your JS syntax.
And it contains some other goodies like various compression tools

它主要是 jslint,它会检查你的 JS 语法。
它包含一些其他的好东西,比如各种压缩工具