源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript中alert()与console.log()的区别

  • 时间:2022-01-03 11:49 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript中alert()与console.log()的区别
[b][1]alert()[/b]     [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 [b][2]console.log()[/b]     [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 [b]alert 和 console.log 的结果不同?[/b]
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的实际执行推迟,相当于“惰性”求值,遇上数组、对象这样的引用类型就出上面的问题了。 [url=https://bugs.webkit.org/show_bug.cgi?id=35801]https://bugs.webkit.org/show_bug.cgi?id=35801[/url] 这是一个很有历史的 BUG,上个月在开发版已经修复了。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部