LoginSignup
790
1120

More than 5 years have passed since last update.

JavaScript,jQueryの爆速コーディング、デバッグ方法論の勧め~実践向け逆引き(windows,chrome向け)~

Last updated at Posted at 2017-03-29

2017/4/218



JavaScript

console.log使
(console.log使)



F12(Elements, Console, Source, Network)
Ctrl+Shift+F(js,css)
HTMLCSSElements
jsSource(Ctrl+S)
js
consolejs()
ajaxNetWork





(一)()

(二)

(三)continue

(四)if

(五)

(六)

(七)javascript

(八)

1.()


EventListeners


便


image
jQuery使
$('p').click(function(){
     hogehoge;
});

だったり、

$('p').on('click',function(){
     hogehoge;
});


Ctrl+Shift+Fclick

HTML

ElementsStyles2
EventListener

jQueryFramework listeners
image
click

2.


ElementsBreak on使

便






$("#hogehoge").append('hogehoge')

って感じで値を変更してくれていればまだ追いやすかったんですが、

id = "#hogehoge"

中略

function bar (id) {
  $(id).append(option);
}



使Break on
ElementsBreak on

image
subtree modification
jQuerystep out

1
$(id).children().remove();

中略

$(id).append(option.attr({ value : 'ほげほげ' })


3. continue


ifdebugger()

console.logalert
OK


Source
if (condition) {
  debugger;
}

後ほど続くconditionのときに見たい処理

と記述しましょう。(Ctrl+Sで保存)debuggerのところにブレークポイントを置いても同じです。

4. ここのif文もし変数がこの値になったらどんな挙動になるの…?

ブレークポイントで止めてConsoleから書き換えてしまおう!

エラー処理とか書いてみたもののそのエラーケースを発生させるのめんどくさいなってことありませんか?

if (text == null) {  // 全然このパターン起こせない
 エラー処理!!!
}

そんなときはエラー処理の上にブレークポイントを置いてそこでとまったときにConsoleから

text = null


(Consoletext)

5. 


console.trace使




Source
console.trace();

と記述しておきましょう。(Ctrl+Sで保存)

console.trace
hogehoge   @ hoge.js:10
fugafuga   @ fuga.js:3

Console

6. 


console.table使



image

console.table(hoge)Console

image

7. javascript










window.onerror = function(msg,url,line){
  $.ajax({
    ...中略...
  });
}

とか

if (condition) {
  $.ajax({
    ...中略...
  });
}

とかいうデバッグ用のコードを仕込んで、
データをajaxでサーバーに送って、再現の方法を直接ユーザに教えてもらうのもアリかもしれませんね。
リリースが必要ですが。

8. オンロード時のデバッグただのブレークポイントだとめんどくさい…

条件付ブレークポイントを使おう!

Sourceからjsを修正して

if (hoge) {
  debugger;
}

F5

使

image.png
Source
Add conditional breakpoint

image.png





使



Web



Web
790
1120
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

790
1120