11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

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

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

if (debugging)<br />

alert("Returned from swapImage() with swapStatus="<br />

and include alert()s marking the flow of execution in swapImages(). By examining the content<br />

and order of the alert()s as they appear, you are granted a window to the internal state of your<br />

script.<br />

Because using many alert()s when debugging large or complicated scripts may be impractical<br />

(not to mention annoying), output is often sent to another browser window instead. Using this<br />

technique, a new window, say, debugWindow, is opened at the beginning of the script, and<br />

debugging information is written into the window using syntax like<br />

debugWindow.document.write() method. <strong>The</strong> only potential gotcha is that you need to wait<br />

for the window to actually be opened before attempting to write() to it. See Chapter 12 for more<br />

information on inter-window communication.<br />

Stack Traces Whenever one function calls another, the interpreter must keep track of the<br />

calling function so that when the called function returns, it knows where to continue execution.<br />

Such records are stored in the call stack, and each entry includes the name of the calling<br />

function, the line number of invocation, arguments to the function, and other local variable<br />

information. For example, consider this simple code:<br />

function a(x)<br />

{<br />

}<br />

document.write(x);<br />

function b(x)<br />

{<br />

}<br />

a(x+1);<br />

function c(x)<br />

{<br />

}<br />

b(x+1);<br />

c(10);<br />

At the document.write in a(), the call stack looks something like this:<br />

a(12), line 3, local variable information…

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

Saved successfully!

Ooh no, something went wrong!