咱们在作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的实际执行推迟,至关于“惰性”求值,赶上数组、对象这样的引用类型就出上面的问题了。