33

Debugging Javascript - 0 to Heisenberg

Embed Size (px)

DESCRIPTION

Debugging Javascript - From 0 to Heisenberg

Citation preview

Page 1: Debugging Javascript - 0 to Heisenberg
Page 2: Debugging Javascript - 0 to Heisenberg

DEBUGGING JAVASCRIPTFROM 0 TO HEISENBERG

Chris MorrowSenior UI Developer

Page 3: Debugging Javascript - 0 to Heisenberg

WALTER WHITE

Page 4: Debugging Javascript - 0 to Heisenberg

HEISENBERG

Page 5: Debugging Javascript - 0 to Heisenberg

JAVASCRIPT BEST PRACTICES (throughout)

BREAKPOINTSWATCHESUSING THE CONSOLECALL STACKCUSTOM TOOLS / UNIT TESTING

THE PLAN

Page 6: Debugging Javascript - 0 to Heisenberg

TOO MANY COOKS INTHE KITCHEN?

Page 7: Debugging Javascript - 0 to Heisenberg

BEST PRACTICES

AVOID GLOBAL VARIABLES<script> var now = new Date(); /* more code goes here */ function doSomething(){ alert('the time is ' + now); }</script>/* code below can overwrite "now" var */<script src="js/dateLibrary.js"></script>

Page 8: Debugging Javascript - 0 to Heisenberg

PROTECT YOUR VARIABLES WITH SCOPE<script>(function(){ // closure function var now = new Date(); /* more code goes here */ function doSomething(){ alert('the time is ' + now); }})();</script>/* code below CAN'T change "now" var */

Page 9: Debugging Javascript - 0 to Heisenberg
Page 10: Debugging Javascript - 0 to Heisenberg

WHY DO PEOPLE HATEDEBUGGING JS?

VARIABLES ARE GLOBAL IN SCOPENO REQUIRED STRONG TYPINGCROSS BROWSER COMPATIBILITY (IE!!!)NO CLASSES

Page 11: Debugging Javascript - 0 to Heisenberg

DEBUGGING GOT YOUSTRESSED?

Page 12: Debugging Javascript - 0 to Heisenberg

BREAKPOINTSNAVIGATING CODESTANDARDCONDITIONAL

Page 13: Debugging Javascript - 0 to Heisenberg

BREAKPOINTSContinue: continues code execution until we encounteranother breakpoint

Step Over: step through code line-by-line to get insights intohow each line affects the variables being updated. Should

your code call another function, the debugger won't jump into itscode, instead stepping over so that the focus remains on thecurrent function (scope).

Step Into: like Step over, however clicking Step into at thefunction call will cause the debugger to move its execution

to the first line in the functions definition.

Step Out: having stepped into a function, clicking this willcause the remainder of the function definition to be run and

the debugger will move its execution to the parent function

Page 14: Debugging Javascript - 0 to Heisenberg

BEST PRACTICES

Page 15: Debugging Javascript - 0 to Heisenberg

WATCHES

Page 16: Debugging Javascript - 0 to Heisenberg

ADDING A WATCH: MULTIPLE WAYSk Sources: select then right-click

k Click "+" on Watch Expressions panel

k Right-click inside Watch Expressions panel

Page 17: Debugging Javascript - 0 to Heisenberg

SCOPE VARIABLESk Like watches, but are only shown in scope

Page 18: Debugging Javascript - 0 to Heisenberg

AND NOW A QUICK MESSAGE FROM OURSPONSOR...

Page 19: Debugging Javascript - 0 to Heisenberg
Page 20: Debugging Javascript - 0 to Heisenberg

USING THE CONSOLEconsole.log()console.assert( expression, object )console.table()Multiline CommandsSnippetsLive Editing

Page 21: Debugging Javascript - 0 to Heisenberg

BEST PRACTICES

DO NOT MINIFY CODE IN DEVELOPMENT

Page 22: Debugging Javascript - 0 to Heisenberg
Page 23: Debugging Javascript - 0 to Heisenberg

CALL STACKBOTTOMS UP

Page 24: Debugging Javascript - 0 to Heisenberg

CRAPPY CODE

Page 25: Debugging Javascript - 0 to Heisenberg

CLEAN UP YOUR CODE

Page 26: Debugging Javascript - 0 to Heisenberg

DEBUGGING TOOLS

ROLL YOUR OWN

Page 27: Debugging Javascript - 0 to Heisenberg

PRE-DEBUGGING TOOLSJSHint / JSLint

JsHint is more lenient than JSLint.

Page 28: Debugging Javascript - 0 to Heisenberg

NOT HAPPY WITHJSHINT?

Page 29: Debugging Javascript - 0 to Heisenberg

CUSTOMIZE IT WITH.jshintrc

Page 30: Debugging Javascript - 0 to Heisenberg

UNIT TESTINGQUnit: http://qunitjs.com/

Mocha: http://visionmedia.github.io/mocha/

Jasmine: http://jasmine.github.io/

Page 31: Debugging Javascript - 0 to Heisenberg

REMOTE TESTINGAndroid - (Chrome Dev Tools)https://developer.chrome.com/devtools

iOS - (Safari Dev Tools)https://developer.apple.com/safari/tools/

Browser Stack: $39.99/month and uphttp://www.browserstack.com/

Adobe Edge Inspect: $9.99/monthhttps://creative.adobe.com/products/inspect

Page 32: Debugging Javascript - 0 to Heisenberg

RESOURCES / QUESTIONS?Chrome Dev Tools:https://developer.chrome.com/devtools

Chrome Dev Tools: Tips & Trickshttps://developer.chrome.com/devtools/docs/tips-and-tricks

jQuery Learning Center:http://learn.jquery.com/javascript-101/

Design Patterns:http://addyosmani.com/resources/essentialjsdesignpatterns/book/

Douglas Crockford: Javascript the Good Partshttp://javascript.crockford.com/

Page 33: Debugging Javascript - 0 to Heisenberg