14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

APPENDIX A ■ Debugging <strong>JavaScript</strong><br />

Figure A-13. You can use the console to inspect <strong>JavaScript</strong> that has been loaded from your site<br />

As you write <strong>JavaScript</strong>, there is one comm<strong>and</strong> you might use more than any other to make sure that things are<br />

working the way you think they should:<br />

console.log();<br />

You used this in the try/catch example. If you have the console open, you can see the results of those statements<br />

printed in the console. In other languages, this would be a print statement, or trace in ActionScript. What this gives<br />

you is the ability to print messages.<br />

For example, suppose you need to know your code executed up to a certain point or the current value of a<br />

variable (showing image x of 10). You can do this <strong>with</strong>out stopping the application from running, as you would do if<br />

you were using a breakpoint.<br />

JSLint <strong>and</strong> Jasmine<br />

Some browser-independent tools are available that also can help you <strong>with</strong> <strong>JavaScript</strong> development. One is the online<br />

<strong>JavaScript</strong> verifier JSLint by Douglas Crockford, which is available at http://www.jslint.com/lint.html. JSLint is<br />

a tool written in <strong>JavaScript</strong> that verifies scripts in terms of syntactical validity <strong>and</strong> tries to ensure good coding style.<br />

Because “good coding style” is a subjective matter, you might want to take JSLint reports <strong>with</strong> a grain of salt. <strong>JavaScript</strong><br />

development happens mostly in browser environments, <strong>and</strong> from time to time you need to bend the rules or cut<br />

corners to make a script work faster or work around browser issues. That being said, it is still a great tool to use to find<br />

ways to optimize your scripts in terms of cleanliness of code if you don’t have to cut corners. The great thing about<br />

JSLint is that it gives you a full analysis of your script, including reports on global variables <strong>and</strong> how many times<br />

different functions have been called.<br />

358<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!