问题
I have some client-side JavaScript code and want to check that file for errors/warnings.
What is the easiest way to check my JavaScript file for errors?
回答1:
Copy and paste it into http://www.jslint.com/ but be prepared to "have your feelings hurt".
回答2:
Opening your JavaScript console in the Chrome browser with (Tools->JavaScript console) or (CTRL + SHIFT + J) is always a good place to start. However, JSLint is really your best bet.
回答3:
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.
To try it out you can copy and paste your code into the online Closure Compiler Service.
I noticed a lot of people suggesting JSLint. I just stumbled on JSHint from a tweet from John Resig that looks super flexible for enforcing coding conventions.
回答4:
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
I'm dropping some small JS/jQuery calls+functions into my php and the esprima checker fits the bill for me.
回答5:
Use something like FireBug for Firefox.
回答6:
Firebug for firefox:
Use Firefox browser to download and install the add-on: https://getfirebug.com/downloads/
If you have an older version of Firefox, click the download older versions, and single click on the .xpi
file to install. Pick the one with the most recent date compatible with your version of firefox.
Put this code into a file called 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:
回答7:
There are two ways to find issues:
Static analysis You could use jshint/jslint with gruntjs builder to identify mostly syntax issues. https://github.com/gruntjs/grunt-contrib-jshint
Runtime error management You 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.
来源:https://stackoverflow.com/questions/5279226/how-to-check-client-side-javascript-code-for-errors