<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
//当使用一个值为undefined的变量时,如果用var关键字定义了,但是没有初始化,用这个变量没问题
//但是如果没有用var关键字定义,直接使用就会报错(只测试了google浏览器)
//但是如果访问一个对象的某个不存在的属性,就不会报错。
if (this.o1) {
console.log("o1为true");
} else {
console.log("o1为false");
}
//显示类型转换函数,String(),Boolean(),Number(),Object(),转换规则与隐式类型转换完全相同
//将null通过String函数转换成string
var result = String(null);
console.log("将null转换成string,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (null == result));
//将undefined通过String函数转换成string
result = String(undefined);
console.log("将undefined转换成string,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:"
+ (undefined == result));
//将NaN通过String函数转换成string
result = String(NaN);
console.log("将NaN转换成string,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (NaN == result));
//将true通过String函数转换成string
result = String(true);
console.log("将true转换成string,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (true == result));
//将null通过Number函数转换成number
result = Number(null);
console.log("将null转换成number,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (null == result));
//将undefined通过Number函数转换成number
result = Number(undefined);
console.log("将undefined转换成number,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:"
+ (undefined == result));
//将true通过Number函数转换成number
result = Number(true);
console.log("将true转换成number,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (true == result));
//将"123"通过Number函数转换成number
result = Number("123");
console.log("将'123'转换成number,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + ("123" == result));
//将"12a"通过Number函数转换成number
result = Number("12a");
console.log("将'12a'转换成number,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + ("12a" == result));
//将null通过Boolean函数转换成boolean
result = Boolean(null);
console.log("将null转换成boolean,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (null == result));
//将undefined通过Boolean函数转换成boolean
result = Boolean(undefined);
console.log("将undefined转换成boolean,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:"
+ (undefined == result));
//将null通过Object函数转换成object,最后会返回一个空的对象{}
result = Object(null);
console.log("将null转换成object,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (null == result));
//将undefined通过Object函数转换成object,最后会返回一个空的对象{}
result = Object(undefined);
console.log("将undefined转换成object,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:"
+ (undefined == result));
//将123通过Object函数转换成object,其实就相当于result = new Number(123);
result = Object(123);
console.log("将123转换成object,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (123 == result));
//将true通过Object函数转换成object,其实就相当于result = new Boolean(true);
result = Object(true);
console.log("将true转换成object,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + (true == result));
//将"123"通过Object函数转换成object,其实就相当于result = new String("123");
result = Object("123");
console.log("将'123'转换成object,转换后类型为:" + (typeof result) + ",值为:" + result + ",转换前后的值是否相等:" + ("123" == result));
</script>
</head>
<body>
</body>
</html>
javascript基础知识梳理-显示类型转换
最新推荐文章于 2021-10-07 23:00:36 发布