Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách thêm code debug vào JavaScript

Thêm code debug bằng alert() hoặc document.write() / console.log() (khuyên dùng).

Ví dụ

var debugging = true;
var whichImage = "widget";
if (debugging) alert("Calls swapImage() with arg: " + whichImage);
var swapStatus = swapImage(whichImage);
if (debugging) alert("Exits swapImage() with swapStatus=" + swapStatus);

Quan sát thứ tự và nội dung alert/console.log để theo dõi luồng chương trình, giá trị biến.