js运算符隐式类型转换
2016-07-26 16:28
337 查看
js运算符隐式类型转换
总结:
(1)x + "" //等价于 String(x)
(2)+ x //等价于 Number(x),也可以写成x-0
(3)!!x //等价于 Boolean(x),注意是双叹号!
js代码
var now = new Date(); //创建一个日期对象
console.log(now);
var a = typeof(now + 1) //=> "string":"+"将日期转换为字符串
var b = typeof(now - 1)
console.log(a+"--"+b); //=> "number":"-"使用对象到数字的转换
now == now.toString()
console.log(now == now.toString()); //=> true:隐式(==)的和显式(toString)的字符串转换
now > (now - 1)
console.log(now+1);
console.log(+now);
console.log(now-0);
console.log(now > (now - 1) );
以上在控制台分别输出
Date {Tue Jul 26 2016 16:33:18 GMT+0800} test.html (第 9 行)
string--number test.html (第 12 行)
true test.html (第 14 行)
Tue Jul 26 2016 16:33:18 GMT+08001 test.html (第 16 行)
1469521998018 test.html (第 17 行)
1469521998018 test.html (第 18 行)
true
总结:
(1)x + "" //等价于 String(x)
(2)+ x //等价于 Number(x),也可以写成x-0
(3)!!x //等价于 Boolean(x),注意是双叹号!
js代码
var now = new Date(); //创建一个日期对象
console.log(now);
var a = typeof(now + 1) //=> "string":"+"将日期转换为字符串
var b = typeof(now - 1)
console.log(a+"--"+b); //=> "number":"-"使用对象到数字的转换
now == now.toString()
console.log(now == now.toString()); //=> true:隐式(==)的和显式(toString)的字符串转换
now > (now - 1)
console.log(now+1);
console.log(+now);
console.log(now-0);
console.log(now > (now - 1) );
以上在控制台分别输出
Date {Tue Jul 26 2016 16:33:18 GMT+0800} test.html (第 9 行)
string--number test.html (第 12 行)
true test.html (第 14 行)
Tue Jul 26 2016 16:33:18 GMT+08001 test.html (第 16 行)
1469521998018 test.html (第 17 行)
1469521998018 test.html (第 18 行)
true
相关文章推荐
- 廖雪峰JS教程--条件判断
- js的大小写敏感性
- js注意事项10
- 关于js的一点总结
- 深入理解javascript函数系列第三篇——属性和方法
- js中new 一个对象内部发生了什么?
- js中比较两个数组中是否含有相同的元素,可去重,可删除合并为新数组
- javaScript 保留2位小数 多余截掉
- 基于JS高级编程的一些总结
- js注意事项09
- JavaScript ---- 懒加载demo
- 廖雪峰JS教程--对象
- Javascript 二维数组去重
- 如何解决jsp:include标签在包含html文件时遇到的乱码问题
- JS模板laytpl
- 时间处理总结(三)javascript与WCF
- js去掉字符串中的空格---用正则表达式;
- 转:js中this关键字详解
- javascript 0000000序列递增算法
- js图片上传预览