JavaScript調(diào)試技巧

快速尋找DOM元素

DOM elements

以表格的形式顯示數(shù)據(jù)對象

var animals = [
 { animal: 'Horse', name: 'Henry', age: 43 },
 { animal: 'Dog', name: 'Fred', age: 13 },
 { animal: 'Cat', name: 'Frodo', age: 18 }
];
console.table(animals);

將輸出......

Console.table

得到一個函數(shù)的堆棧軌跡(某個時間的調(diào)用堆棧狀態(tài))

var car;
var func1 = function(){
    func2();        
}
 
var func2 = function(){
    func4();
}
 
var func3 = function(){
    
}
 
var func4 = function(){
    car = new Car();
    car.funcX();
}
 
var Car = function(){
    this.brand = 'volvo';
    this.color = 'red';

    this.funcX = function(){
        this.funcY();
    }
 
    this.funcY = function(){
        this.funcZ();
    }
 
    this.funcZ = function(){
        console.trace('trace car')
    }
}
func1();

將輸出......

CONSOLE TRACE

在復(fù)雜的調(diào)試中發(fā)現(xiàn)重要信息

console.todo = function( msg){
    console.log( '%c %s %s %s ', 'color: yellow; background-color: black;', '--', msg, '--');
}
console.important = function( msg){
    console.log( '%c%s %s %s', 'color: brown; font-weight: bold; text-decoration: underline;', '--', msg, '--');
}
console.todo("This is something that's need to be fixed");
console.important('This is an important message');

在上述代碼中 %s 應(yīng)用于字符串, %i 應(yīng)用于整型 %c 應(yīng)用于CSS樣式.
將輸出......

Custom console message
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容