Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
JavaScript Debugging
Search
Amjad Masad
May 05, 2014
Programming
11
1.8k
JavaScript Debugging
Amjad Masad
May 05, 2014
Tweet
Share
More Decks by Amjad Masad
See All by Amjad Masad
fb-flo
amasad
0
170
Modern Web Development Tools And Workflow
amasad
0
210
Other Decks in Programming
See All in Programming
LangGraphでのHuman-in-the-Loopの実装
os1ma
3
930
サーバーレスで負荷試験!Step Functions + Lambdaを使ったk6の分散実行
shuntakahashi
5
1.2k
LangChainの現在とv0.3にむけて
os1ma
4
790
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
1
190
今インフラ技術をイチから学び直すなら
yuhta28
1
120
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
460
TypeScriptで 負荷テストを書こう 〜k6のシングルバイナリの秘密〜
dora1998
7
3k
Kotlin 2.0 and Beyond
antonarhipov
2
140
Understand the mechanism! Let's do screenshots tests of Compose Previews with various variations / 仕組みから理解する!Composeプレビューを様々なバリエーションでスクリーンショットテストしよう
sumio
2
290
dRuby 入門者によるあなたの身近にあるdRuby 入門
makicamel
4
350
ブラウザ互換の重要性 - あらゆるユーザーに価値を届けるために必要なこと
yamanoku
0
110
ESLint Rule により事業, 技術ドメインに沿った制約と誓約を敷衍させるアプローチのすゝめ
shinyaigeek
1
2.9k
Featured
See All Featured
A better future with KSS
kneath
235
17k
Fashionably flexible responsive web design (full day workshop)
malarkey
401
65k
Adopting Sorbet at Scale
ufuk
73
8.9k
Rails Girls Zürich Keynote
gr2m
93
13k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5k
In The Pink: A Labor of Love
frogandcode
139
22k
The Straight Up "How To Draw Better" Workshop
denniskardys
230
130k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
27
8.9k
Agile that works and the tools we love
rasmusluckow
327
20k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Making the Leap to Tech Lead
cromwellryan
128
8.8k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Transcript
JAVASCRIPT DEBUGGING AMJAD MASAD ! facebook
HOW FAR HAVE WE COME
None
IMMEDIATE
<script>! ! document.write('Hello World’);! ! </script>
SRSLY?
LOOK CLOSER
WTF
WE’VE COME A LONG WAY
• WORK AT FACEBOOK & BEFORE THAT WAS FIRST ENGINEER
AT CODECADEMY • JAVASCRIPT EXPERIMENTALIST • MADE REPL.IT, DEBUG.JS, SOUNDOFJS etc • amasad.me, @amasad ABOUT ME
• CODE THAT’S EASIER TO DEBUG • DEBUGGING TIPS AND
TRICKS • THE TOOLS JAVASCRIPT DEBUGGING
CODE EASIER TO DEBUG
LAMBDAS ARE FUN BUT ARE ALSO HARD TO DEBUG
CONTRIVED EXAMPLES AHEAD
CLOSURES HIDE THEIR STATE REALLY WELL function counter(value) {! !
return function() {! ! ! value++;! ! ! return value;! ! };! } class Counter {! ! constructor(val) {! ! ! this.value = val;! ! }! ! increment() {! ! ! this.value++;! ! ! return this.value;! ! }! }
CAN’T MONKEY PATCH CLOSURES counter.increment = function() {! ! this.value--;!
! return this.value;! }
PARTIAL APPLICATIONS AND LAZY EVALUATION ARE EVEN HARDER TO DEBUG
function counter(value) {! ! return function() {! ! ! return value;! ! };! }! ! function increment(counter) {! ! return function() {! ! ! return counter() + 1;! ! };! }! ! var count = counter(2);! count = increment(count);! console.log(count());
MAKE CLASSES AND OBJECTS ACCESSIBLE FROM THE CONSOLE
LOOK AT APPLICATION STATE WHILE IN IDLE STATE > app.counter.value!
1
START THINGS FROM THE CONSOLE > app.user.emit('change')
QUICKLY SET BREAKPOINTS AND MONKEY PATCH FROM THE CONSOLE >
debug(app.user.save);
DEBUGGING TIPS AND TRICKS
ALWAYS BE DEBUGGING • HAVE THE PROGRAM IN A RUNNABLE
STATE • SPLIT UP BIG CHANGES INTO SMALLER STEPS • BREADTH FIRST > DEPTH FIRST • KEEPS THE MOMENTUM GOING
CHANGE AND BREAK THINGS • RUNNING THE PROGRAM OFTEN ALLOWS
YOU TO TAKE BOLD STEPS • WHILE IN THE DEBUGGER CHANGE THINGS • LEAN ON YOUR TOOLS
LIVE EDIT • FIX MISTAKES WITHOUT RELOADING • IMPLEMENT FEATURES
WITHOUT RELOADING • ADD CONDITIONAL DEBUG STATEMENTS
SETTING UP TRAPS • IN LARGE APPLICATIONS AND TEAMS IT’S
HARD TO KNOW WHAT EVERYONE IS DOING • CAN’T STEP-DEBUG ALL THE CODE • NEED A WAY TO BREAK ON ACTIONS
BREAK ON METHOD CALLS Event.prototype.preventDefault = function() {! ! debugger;!
! return _original.apply(this, arguments);! } • CAN’T SET A BREAKPOINT ON NATIVE METHODS • NEED TO KNOW SOURCE FOR METHOD
BREAK ON CUSTOM EVENTS monitorEvents(elem);! ! user.on('change', function() {! debugger;!
}); • WHEN AN EVENT IS FIRED, I WANT STOP AND INSPECT • NEED TO MAKE SURE AN EVENT IS FIRING
BREAK ON PROPERTY ACCESS • WTF CHANGES • SETTERS AND
GETTERS counter.__defineSetter__(! ! 'value',! ! function() { debugger; }! );
BREAK ON CALLBACKS • SIMILAR TO CUSTOM EVENTS • TO
MAKE SURE AN ASYNC PROCESS FINISHES user.save(function() {! ! debugger;! });
BREAK ON DOM MUTATION • CAN BE DONE FROM DEVTOOLS
• FINE GRAINED CONTROL • ASYNC CALL STACK
BREAK ON OBJECT MUTATION • Object.observe! • BREAK ON PROPERTY
ADD, CHANGE, DELETE • ASYNC CALL STACK (NOT WORKING YET)
THE TOOLS
THE COMMAND LINE API $0! $(selector)! monitorEvents(elem)! getEventListeners(elem)
UNDOCUMENTED APIS debug(fn);! montior(fn);
DEBUG UTILS $duv(object, event);! $dug(object, property);! $dus(object, property);! $dum(object, method);!
! and others… github.com/amasad/debug_utils
PROBLEMS WITH LIVE EDIT • LIVE EDIT IN CHROME IS
NOT FLEXIBLE • CAN’T HAVE A BUILD STEP • CAN’T HAVE A REMOTE FILE SYSTEM • LOCKED INTO THE DEVTOOLS EDITOR
INTRODUCING flo • OPEN SOURCE TOOL BY FACEBOOK • LIVE
EDIT JAVASCRIPT, CSS, IMAGES • SUPPORTS BUILD STEPS AND ANY DEV ENV • EASY TO INTEGRATE AND HACKABLE
WHY flo • I JOINED FACEBOOK LAST YEAR • A
LOT OF JAVASCRIPT GOODNESS • RELOADING AND GETTING TO THE USER FLOW TAKES A LONG TIME • COMPLEX DEV ENVIRONMENT
HOW flo • SERVER COMPONENT IS NPM MODULE • CLIENT
COMPONENT IS CHROME EXTENSION • facebook.github.com/fb-flo
THANK YOU