javascript中alert()与console.log()的区别
2015-08-26 09:02
886 查看
[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,上个月在开发版已经修复了。
您可能感兴趣的文章:
- JavaScript 输出显示内容(document.write、alert、innerHTML、console.log)
- 利用Js的console对象,在控制台打印调式信息测试Js的实现
- javascript代码调试之console.log 用法图文详解
- js console.log打印对像与数组用法详解
- JS中捕获console.log()输出的方法
- JavaScript中的console.dir()函数介绍
- JavaScript中的console.trace()函数介绍
- JavaScript中的console.profile()函数详细介绍
- Javascript的console['''']常用输入方法汇总
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- JavaScript拆分字符串时产生空字符的原因
- IE8开发人员工具教程(二)
- 在flex中执行一个javascript方法的简单方式
- Flex结合JavaScript读取本地路径的方法
- PowerShell中执行Javascript的方法示例
- javascript asp教程第六课-- response方法
- javascript asp教程More About Recordsets
- javascript asp教程第十二课---session对象
- javascript asp教程创建数据库连接
- javascript asp教程错误处理