2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptのデバッグメモ

Posted at

概要

JSConfの動画を見て、便利そうだと思ったので個人的メモします。
動画はこちら

Chrome DevTools

console.log

変数をログで吐き出す時に、オブジェクトにするとキー・バリューで見れる。

const name = 'Hiroshi';
const age = 40;

console.log({ name, age });
Screen Shot 2019-10-23 at 22.20.56.png

色付け

console.log(`%c${name}`, 'background-color: red; color: white;');
Screen Shot 2019-10-24 at 10.17.17.png

※残念ながらオブジェクトは[object Object]になります。
Screen Shot 2019-10-24 at 10.19.54.png

console.table

const people = [
    { id: 1, name: 'Hiroshi', age: 40 },
    { id: 2, name: 'Tomozo', age: 76 },
    { id: 3, name: 'Momoko', age: 9 }
];

console.table(people);

テーブル形式で出力
Screen Shot 2019-10-24 at 10.21.24.png

console.group

function personData(person) {
    console.group('person');
    console.log(`name: ${person.name}`);
    console.log(`age: ${person.age}`);
    console.groupEnd();
}

const person = {name: 'Hiroshi', age: 40};
personData(person);

ログ出力を見やすく出来る。
Screen Shot 2019-10-24 at 10.22.52.png

Consoleタブ

Elementsタブで選択しているDOMエレメントを出力
現在選択しているものは$0で表示出来る。
$1〜$4まで履歴が見れる。
Screen Shot 2019-10-24 at 10.27.11.png

Consoleタブでのエレメントの取得

$('selector') = document.querySelector('selector')
$$('selector') = document.querySelectorAll('selector')
Screen Shot 2019-10-24 at 10.28.58.png

console.trace

関数がどこから呼ばれているかをトレース出来る。

function first() {
    console.log('first');
    console.trace();
}

function second() {
    console.log('second');
    first();
}

function third() {
    console.log('third');
    second();
}

third();
Screen Shot 2019-10-24 at 10.32.23.png

console.dir

const el = document.querySelector('div');
console.log(el)
console.dir(el);

エレメントのプロパティが全部表示される。
プロパティの書き換え可能
Screen Shot 2019-10-24 at 10.35.32.png

Node.js環境だとネストしたオブジェクトが内部まで表示可能。

const obj = {
    a: {
        b: {
            c: {
                d: {
                    e: {
                        f: { 
                            value: 1,
                            msg: 'Hello, world!'
                        }
                    }
                }
            }
        }
    }
}

console.log(obj);
console.dir(obj, { depth: null });
Screen Shot 2019-10-24 at 10.37.57.png

参考

Essential JavaScript debugging tools for the modern detective by Rebecca Hill | JSConf Budapest 2019

2
3
0

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?