JavaScript/Notes/Debugging: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
No edit summary
No edit summary
Line 21: Line 21:




=== FIXME: Find and fix the bug ===
The code example below must be changed to output:
<pre>
Check the value of i: 0
Check the value of i: 1
Check the value of i: 2
Check the value of i: 3
</pre>
<strong>Example</strong>
<source lang="javascript">
var count = 0;
var output = document.getElementById("output");
for ( var i = 0; i < 4; i++ ) {
  setTimeout(function(){
    output.textContent = "Check the value of i:" + i;
  }, 200);
}
</source>


=== debugger from the menu ===
=== debugger from the menu ===
Firefox:
[[File:Debugger.png]]
[[File:Debugger.png]]



Revision as of 11:30, 1 January 2014

It Doesn't Work!

How to Launch the Debugger

A few approaches:

debugger keyword

<source lang="javascript"> function myFunc(myParam) {

 debugger;
 var myVar = 1;
 
 function innerFunc() {
 
 }

} </source>


FIXME: Find and fix the bug

The code example below must be changed to output:

Check the value of i: 0
Check the value of i: 1
Check the value of i: 2
Check the value of i: 3

Example <source lang="javascript"> var count = 0; var output = document.getElementById("output");

for ( var i = 0; i < 4; i++ ) {

 setTimeout(function(){ 
   output.textContent = "Check the value of i:" + i; 
 }, 200); 

} </source>

debugger from the menu

Firefox: Debugger.png

Add a Breakpoint

Debugger-breakpoint.png


Debugger Keyword Full Example

Copy'n'paste this example into your text editor, open your browser, and open this file. <source lang="html5"> <!doctype html> <head> <title>Debugger Example</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body>

Test Debugger

When Entering an Execution Context, the Variable Environment is initialized with parameter variables, Function Declarations, and Variable Statements.

function myFunc(myParam) {
  debugger;
  var myVar = 1;
  
  function innerFunc() {
  
  }
}

<script> function myFunc(myParam) {

 debugger;
 var myVar = 1;
 
 function innerFunc() {
 
 }

} </script>

Open the debugger and then click the button below.

<button onclick="myFunc(7)">myFunc(7)</button> </body> </html> </source>