咱們在作js調試的時候使用 alert 能夠顯示信息,調試程序,alert 彈出窗口會中斷程序, 若是要在循環中顯示信息,手點擊關閉窗口都累死。並且 alert 顯示對象永遠顯示爲[object ]。 本身寫的 log 雖然能夠顯示一些 object 信息,但不少功能支持都沒有 console 好html
[1]alert()chrome
[1.1]有阻塞做用,不點擊肯定,後續代碼沒法繼續執行數組
[1.2]alert()只能輸出string,若是alert輸出的是對象會自動調用toString()方法優化
e.g. alert([1,2,3]);//'1,2,3'spa
[1.3]alert不支持多個參數的寫法,只能輸出第一個值調試
e.g. alert(1,2,3);//1code
[2]console.log()htm
[2.1]在打印臺輸出對象
[2.2]能夠打印任何類型的數據blog
e.g. console.log([1,2,3]);//[1,2,3]
[2.3]支持多個參數的寫法
e.g. console.log(1,2,3)// 1 2 3
alert 和 console.log 的結果不一樣?
score = [1,2,3]; sortedScore = []; console.log(score); sortedScore = score.sort(sortNumber) console.log(sortedScore); function sortNumber(a, b) { return b - a; }
以上輸出:
[3, 2, 1]
[3, 2, 1]
可是改爲alert:
score = [1,2,3]; sortedScore = []; alert(score); sortedScore = score.sort(sortNumber) alert(sortedScore); function sortNumber(a, b) { return b - a; }
以上輸出:
1, 2, 3
3, 2, 1
爲何會這樣?不該該都是:
1, 2, 3
3, 2, 1
嗎?
通過一番研究發現是chrome實現的問題,對輸出作了不太合適的優化,把console.log的實際執行推遲,至關於「惰性」求值,趕上數組、對象這樣的引用類型就出上面的問題了。