如何检查客户端 JavaScript 代码是否有错误?

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

How to check client-side JavaScript code for errors?

javascriptjavascript-debugger

提问by Steven Hammons

I have some client-side JavaScript code and want to check that file for errors/warnings.

我有一些客户端 JavaScript 代码,想检查该文件是否有错误/警告。

What is the easiest way to check my JavaScript file for errors?

检查我的 JavaScript 文件是否有错误的最简单方法是什么?

回答by Shaggy Frog

Copy and paste it into http://www.jslint.com/but be prepared to "have your feelings hurt".

将其复制并粘贴到http://www.jslint.com/ 中,但要做好“让您的感情受到伤害”的准备。

回答by Yottagray

Opening your JavaScript console in the Chrome browser with (Tools->JavaScript console) or (CTRL + SHIFT + J) is always a good place to start. However, JSLintis really your best bet.

在 Chrome 浏览器中使用 (Tools->JavaScript console) 或 (CTRL + SHIFT + J) 打开 JavaScript 控制台总是一个不错的起点。但是,JSLint确实是您最好的选择。

回答by PPC-Coder

I'd reccomend trying out the Google Closure Compiler. Not only can it check your code for errors it can also perform some optimizations. "Compiler" here might be a bit of a misnomer since it takes in JavaScript and outputs back JavaScript.

我建议尝试使用Google Closure Compiler。它不仅可以检查您的代码是否有错误,还可以执行一些优化。这里的“编译器”可能有点用词不当,因为它接收 JavaScript 并输出 JavaScript。

To try it out you can copy and paste your code into the online Closure Compiler Service.

要试用它,您可以将代码复制并粘贴到在线Closure Compiler Service 中

I noticed a lot of people suggesting JSLint. I just stumbled on JSHintfrom a tweetfrom John Resig that looks super flexible for enforcing coding conventions.

我注意到很多人建议使用 JSLint。我刚刚从 John Resig 的一条推文中偶然发现了JSHint,它在执行编码约定方面看起来非常灵活。

回答by KayCee

JSLint et all are great if you really worry about tabs and spaces, but for JS syntax I use http://esprima.org/demo/validate.html

如果您真的担心制表符和空格,JSLint 等都很棒,但是对于 JS 语法,我使用http://esprima.org/demo/validate.html

I'm dropping some small JS/jQuery calls+functions into my php and the esprima checker fits the bill for me.

我将一些小的 JS/jQuery 调用+函数放入我的 php 中,esprima 检查器适合我。

回答by Eric Leschinski

Firebug for firefox:

火狐的萤火虫:

Use Firefox browser to download and install the add-on: https://getfirebug.com/downloads/

使用 Firefox 浏览器下载并安装插件:https: //getfirebug.com/downloads/

If you have an older version of Firefox, click the download older versions, and single click on the .xpifile to install. Pick the one with the most recent date compatible with your version of firefox.

如果您有旧版本的 Firefox,请单击下载旧版本,然后单击.xpi文件进行安装。选择与您的 Firefox 版本兼容的最新日期。

Put this code into a file called whatever.html:

将此代码放入名为 的文件中whatever.html

<script type="text/javascript">
    var successFunction(){
    }
</script>

Open that file with firefox. Open the firebug window. Click the Console tab. Enable it. And it should tell you that the code above code has a error:

用火狐打开那个文件。打开萤火虫窗口。单击控制台选项卡。启用它。它应该告诉你上面的代码有一个错误:

enter image description here

在此处输入图片说明

回答by Alexey Petushkov

There are two ways to find issues:

有两种方法可以找到问题:

  • Static analysisYou could use jshint/jslint with gruntjs builder to identify mostly syntax issues. https://github.com/gruntjs/grunt-contrib-jshint

  • Runtime error managementYou can catch errors and logs from your clients with logging services like a JsLog.me. Each browser have its own runtime environment for JavaScript, so client logging catching real-world issues.

  • 静态分析您可以使用 jshint/jslint 和 gruntjs builder 来识别主要的语法问题。 https://github.com/gruntjs/grunt-contrib-jshint

  • 运行时错误管理您可以使用JsLog.me 之类的日志记录服务从客户端捕获错误和日志。每个浏览器都有自己的 JavaScript 运行时环境,因此客户端日志记录捕捉现实世界的问题。

回答by Prisoner

Use something like FireBug for Firefox.

使用类似 FireBug for Firefox 的东西。