debugging javascript (by thomas bindzus, founder, vinagility & thanh loc vo, cto, epsilon mobile...

26
Debugging JavaScript Client side scripting

Upload: javascript-meetup-hcmc

Post on 07-Nov-2014

789 views

Category:

Technology


1 download

DESCRIPTION

Topic: Debugging JavaScript through Developer Tools in Google Chrome, FireBug in FireFox, and on Safari - Speakers: Thomas Bindzus, Founder, Vinagility Thanh Loc Vo, CTO, Epsilon Mobile

TRANSCRIPT

Page 1: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Debugging JavaScriptClient side scripting

Page 2: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Outline

1. Debugging toolsa. Built-ins and beyond, but mostly built-ins

2. Fishbone diagram

3. Debugging workshop

Page 3: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Getting started with debugging JS

1. Brower built-insa. Chrome, Safari, Opera, Firefox, IE

2. Browser extensiona. Firebug (lite) (Firefox, IE6+, Opera, Safari, Chrome)

3. External debugger applicationa. Weinre

BIAS ALERT!!

Page 4: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

The code we’ll be using

● index.html○ Gluing it all together

● script.js○ Declaration of our DebuggerExamples object

● style.css○ Nothing stylish :-)

Page 5: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Breaking the code - Statically

Static breakpoints using the debugger keyword…breakthru: function() { // Queen debugger; …}… Don’t do this!

Page 6: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Show me the code!

Page 7: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Breaking the code - Dynamically

Dynamic breakpoints

Best practice!

Page 8: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

The pain of minified JavaScript

But my JavaScript is minified I can’t break in it?!

YES YOU CAN! :-)

Page 9: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

The JavaScript GPS

● Navigate the code● Get your position● Breakpoints

○ Code○ DOM○ XHR○ Event listener

?

Page 10: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

The console.log command

● console.log('Most developers know this')

● console.log('And even ', this)

● console.log('What %s this %d?', 'about', 1)

● What is the output?!

Page 11: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Some more console commandsCommand What is the result?

inspect >inspect(document.body)

$0 >$0

$_ >$_

copy >copy($_)

dir >dir($0)

clear >clear()Bonus question (given that jQuery is present):

>inspect($)

Page 12: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Want to learn more?

● Some resources○ https://developers.google.com/chrome-developer-tools/docs/tips-and-tricks

○ http://www.paulirish.com/2011/a-re-introduction-to-the-chrome-developer-tools/

○ http://www.paulirish.com/2011/quick-color-manipulation-with-the-chrome-devtools/

○ http://www.randomthink.net/blog/2012/11/breakpoint-actions-in-javascript/

○ http://www.youtube.com/watch?v=nOEw9iiopwI

○ http://www.youtube.com/watch?v=N8SS-rUEZPg&feature=youtu.be

● And of course much of the same is possible in Firebug

○ http://lmgtfy.com/?q=Firebug+tips+and+tricks

Page 13: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Fishbone diagram

● Other name: Ishikawa diagram.● Used for cause and effect analysis.

Page 14: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Fishbone diagram (2)

Page 15: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Fishbone diagram (3)1. Collect all the effect of the errors.2. With each effect, identify all the possible causes.3. Analyze the causes, just to group and prioritize them.4. Pick a cause from the list in prioritized order, make a

solution, then implement it. 5. If the error wasn’t been solved, back to step #4.

Page 16: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Pros for using the process?

● Help you to have an overview of the problem.

● Easier to call for help, as well as better collaboration.

● When you can’t discover the issue, you know why. My code broke, and I don’t know why...

Page 17: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Cons

Take times….

Page 18: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Questions?

Page 19: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Workshop

Page 20: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Problem #1

http://jsfiddle.net/jECLf/1/

Page 21: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Problem #2 http://jsfiddle.net/t45UM/

Page 22: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Problem #3

What is the problem, and how to fix?http://jsfiddle.net/Q7vhS/

http://jsfiddle.net/Q7vhS/embedded/result/

Page 23: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Problem #4var elements = [1,2,3,4,5,6,7,8,9];

$(elements).each(function(i, e) {

var row;

if (i % 3 == 0) {

row = $('<div>').appendTo('body');

}

$('<span>' + e + '</span>').appendTo(row);

});

I want to create a grid with 3 elements

in each row. This code relies on

jQuery, but there is something wrong,

can you spot it?

Page 25: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Presentation made for “Javascript Ho Chi Minh City” meetup group

You can find us at:● http://meetup.com/JavaScript-Ho-Chi-Minh-City/● https://www.facebook.com/JavaScriptHCMC● https://plus.google.

com/communities/116105314977285194967

Page 26: Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, CTO, Epsilon Mobile )

Thank you