Must Know Dev Tools Tricks


Posted by wayne201299 on 2023-09-27

Google Chrome開發者工具的一些小技巧介紹

inspect

可以透過inspect了解HTML中的element,也可以針對element的不同event下中斷點檢查

console

印出結果的幾種方式

    // Regular
    console.log("Hello");

    // Interpolated
    console.log("Hello I am a %s string!", "💩");

    // Styled
    console.log("%c I am some great text", "font-size: 50px; background: red; text-shadow: 10px 10px 0 blue");

    // warning!
    console.warn("Oh No!");

    // Error :|
    console.error("Shit!");

    // Info
    console.info("Crocodiles eat 3-4 people per year");

    // Testing
    const p = document.querySelector('p');
    console.assert(p.classList.contains('ouch'), "That is wrong!");

     // table
    console.table(dogs);

檢查DOM element

    // Viewing DOM Elements
    console.log(p);
    console.dir(p);

清除上面所有log

     console.clear();

log分類,在迴圈中設定group key將log分組

    // Grouping together
    dogs.forEach(dog => {
      console.groupCollapsed(`${dog.name}`);
      console.log(`This is ${dog.name}`);
      console.log(`${dog.name} is ${dog.age} years old`);
      console.log(`${dog.name} is ${dog.age * 7} dog years old`);
      console.groupEnd(`${dog.name}`);
    });

計算次數

    // counting
    console.count('Wes');
    console.count('Wes');
    console.count('Wes');
    console.count('Wes');
    console.count('Andy');
    console.count('Wes');
    console.count('Andy');

計算時間,可以當作API效能測試時的依據

    // timing
    console.time('fetching data');
    fetch('https://api.github.com/users/wesbos')
      .then(data => data.json())
      .then(data => {
        console.timeEnd('fetching data');
        console.log(data);
      });

#javascript







Related Posts

反推 - Alpine.js

反推 - Alpine.js

	JS-[forEach篇]-遍歷陣列:for of 與 .forEach()

JS-[forEach篇]-遍歷陣列:for of 與 .forEach()

React 基礎

React 基礎


Comments