1.说数据隐式转换前先说一下typeof
typeof()返回数据的类型有以下几种
1.undefined----如果这个值未初始化或未声明;
2.string---如果这个值是字符串;
3.Boolean----如果这个值是布尔值;
4.number----如果这个值是数值
5.object----如果这个值是对象或null;
6.function----如果这个值是函数。
2.显式的转换数据类型
转换为数字:Number(a)、parseInt(a)、parseFloat(a)
转换为字符串:a.toString() String(a)
转换为布尔型:Boolean(a)
3.隐式的数据转换
var trans = [];
trans[0] = 1 + "true"; //'1true'
trans[1] = 1 + true; //2
trans[2] = 1 + undefined; //NaN
trans[3] = 1 + null; //1
trans[4] = "2" > 10; //false 2>10?
trans[5] = "2" > '10'; //true '2'. charCodeAt()>'10'. charCodeAt()
trans[6] = "abc" > "b"; //false 从左往右比较 'b'>'a'
trans[7] = "abc" > "aad"; //true
trans[8] = NaN == NaN; //false
trans[9] = undefined == null; //true
trans[10] = undefined == undefined; //true
trans[11] = null == null; //true
console.log(trans); //["1true", 2, NaN, 1, false, true, false, true, false, true, true, true]
代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>隐式类型转换</title>
</head>
<body>
</body>
<script>
//1.typeof
// tip:null是一个原始值,在typeof中会认为null是为了给对象占位的 所以是object
var type = [];
type[0] = typeof (a); //undefined----如果这个值未初始化或未声明;
type[1] = typeof (true); //string---如果这个值是字符串;
type[2] = typeof ('hh'); //Boolean----如果这个值是布尔值;
type[3] = typeof (123); //number----如果这个值是数值
type[4] = typeof (new Object); //object----如果这个值是对象或null;
type[5] = typeof (null);
type[6] = typeof (function () {}); //function----如果这个值是函数。
console.log(type); // ["undefined", "boolean", "string", "number", "object", "object", "function"]
</script>
<script>
// 2.显式的转换数据类型
// 转换为数字:Number(a)、parseInt(a)、parseFloat(a)
// 转换为字符串:a.toString() String(a)
// 转换为布尔型:Boolean(a)
var arr = new Array();
arr[0] = Number("9"); //转为数字
arr[1] = Number("a");
arr[2] = Number(undefined);
arr[3] = Number(null);
arr[4] = Number(function () {});
arr[5] = Number(9 > 0);
arr[6] = Number("_123");
arr[7] = parseInt("_123"); //转为整型
arr[8] = parseInt("123px"); //返回连续的数字位
arr[9] = parseFloat("123.2px"); //返回连续的数字位
arr[10] = String(123); //转为字符串
arr[11] = String(undefined);
arr[12] = String(null);
arr[13] = Boolean(123); //转为布尔
arr[14] = Boolean("abc");
arr[15] = Boolean(undefined);
arr[16] = Boolean(null);
arr[17] = Boolean(function () {});
console.log(arr) //[9, NaN, NaN, 0, NaN, 1, NaN, NaN, 123, 123.2, "123", "undefined", "null", true, true, false, false, true]
</script>
<script>
// 3.隐式的数据转换
var trans = [];
trans[0] = 1 + "true"; //'1true'
trans[1] = 1 + true; //2
trans[2] = 1 + undefined; //NaN
trans[3] = 1 + null; //1
trans[4] = "2" > 10; //false 2>10?
trans[5] = "2" > '10'; //true '2'. charCodeAt()>'10'. charCodeAt()
trans[6] = "abc" > "b"; //false 从左往右比较 'b'>'a'
trans[7] = "abc" > "aad"; //true
trans[8] = NaN == NaN; //false
trans[9] = undefined == null; //true
trans[10] = undefined == undefined; //true
trans[11] = null == null; //true
console.log(trans); //["1true", 2, NaN, 1, false, true, false, true, false, true, true, true]
</script>
<script>
//a等于多少的时候能正确打印
var a = {
i: 0,
//对象的valueOf是可以从写对象的
valueOf: function () {
return ++a.i;
}
};
if (a == 1 && a == 2 & a == 3) { //每一次运算都会调用一次a的valueOf()方法
console.log("Why hello there!")
}
//a怎么写能正确打印
</script>
<script>
//a等于多少的时候能正确打印
var n = 1;
Object.defineProperty(window, 'aaaa', {
get: function () {
return n++
}
});
if (aaaa == 1 && aaaa == 2 & aaaa == 3) { //每一次运算都会调用一次a的valueOf()方法
console.log(1)
}
</script>
<script>
// 坑四:逻辑非隐式转换与关系运算符隐式转换搞混淆
console.log([] == 0) //true [].valueOf().tostring==0
console.log(![] == 0) //true
console.log([] == ![]) //true
console.log([] == null) //false
console.log([] == []) //false
console.log({} == !{}) //false {}.valueOf().tostring==[object Object]
console.log({} == {}) //false
// 关系运算符:将其他数据类型转变成数字
// 逻辑非:将其他数据类型用布尔的方法转换成布尔型
// 以下8中转换成布尔会得到false 其他都为true
// 0 ,-0,NaN,undefined,null,'',false,document.all()
</script>
</html>