[1]alert()
[1.1]有阻塞作用,不點擊確定,后續代碼無法繼續執行
[1.2]alert()只能輸出string,如果alert輸出的是對象會自動調用toString()方法
e.g. alert([1,2,3]);//'1,2,3'
[1.3]alert不支持多個參數的寫法,只能輸出第一個值
e.g. alert(1,2,3);//1
[2]console.log()
[2.1]在打印臺輸出
[2.2]可以打印任何類型的數據
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的實際執行推遲,相當于“惰性”求值,遇上數組、對象這樣的引用類型就出上面的問題了。
https://bugs.webkit.org/show_bug.cgi?id=35801
這是一個很有歷史的 BUG,上個月在開發版已經修復了。
新聞熱點
疑難解答