目录
1.背景介绍
2.知识剖析
3.常见问题
4.解决方案
5.编码实战
6.扩展思考
7.参考文献
8.更多讨论
//Boolean() 用于将非逻辑值转换为逻辑值(true 或者 false)。
//检查逻辑对象是 true 还是 false。
var b1=new Boolean(0);
var b2=new Boolean(1);
var b3=new Boolean("");
var b4=new Boolean(null);
var b5=new Boolean(NaN);
var b6=new Boolean("false");
document.write("0 是逻辑的 "+ b1 +"
");//0 是逻辑的 false
document.write("1 是逻辑的 "+ b2 +"
");//1 是逻辑的 true
document.write("空字符串是逻辑的 "+ b3 + "
");//空字符串是逻辑的 false
document.write("null 是逻辑的 "+ b4+ "
");//null 是逻辑的 false
document.write("NaN 是逻辑的 "+ b5 +"
");//NaN 是逻辑的 false
document.write("字符串 'false' 是逻辑的 "+ b6 +"
");//字符串 'false' 是逻辑的 true
//toString() 可把一个逻辑值转换为字符串。
//把一个布尔值转换成字符串
var boo = new Boolean(true)
document.write(boo.toString()) //输出true
//String() 把对象的值转换为字符串。
var test1= new Boolean(1);
var test2= new Boolean(0);
var test3= new Boolean(true);
var test4= new Boolean(false);
var test5= new Date();
var test6= new String("999 888");
var test7=12345;
document.write(String(test1)+ "
");//true
document.write(String(test2)+ "
");//false
document.write(String(test3)+ "
");//true
document.write(String(test4)+ "
");//false
document.write(String(test5)+ "
");//Wed Oct 28 00:17:40 UTC+0800 2009
document.write(String(test6)+ "
");//999 888
document.write(String(test7)+ "
");//12345
//isNaN() 用于检查其参数是否是非数字值。
//检查数字是否非法:
document.write(isNaN(123));//false
document.write(isNaN(-1.23));//false
document.write(isNaN(5-2));//false
document.write(isNaN(0));//false
document.write(isNaN("Hello"));//true
document.write(isNaN("2005/12/12"));//true
//Number() 把对象的值转换为数字。
//我们将尝试把不同的对象转换为数字
var test1= new Boolean(true);
var test2= new Boolean(false);
var test3= new Date();
var test4= new String("999");
var test5= new String("999 888");
document.write(Number(test1)+ "
");//1
document.write(Number(test2)+ "
");//0
document.write(Number(test3)+ "
");//1256657776588
document.write(Number(test4)+ "
");//999
document.write(Number(test5)+ "
");//NaN
//parseInt() 可解析一个字符串,并返回一个整数。
//我们将使用 parseInt() 来解析不同的字符串:
document.write(parseInt("10"));//返回 10
document.write(parseInt("19",10));//返回 19 (10+9)
document.write(parseInt("11",2));//返回 3 (2+1)
document.write(parseInt("17",8));//返回 15 (8+7)
document.write(parseInt("1f",16));//返回 31 (16+15)
document.write(parseInt("010"));//未定:返回 10 或 8
var a = 100; console.log(typeof a); //输出 number
var a = 'hello'; console.log(typeof a); //输出 string
var a = true; console.log(typeof a); //输出 boolean
var a = undefined; console.log(typeof a); //输出 undefined
var a = null; console.log(typeof a); //输出 object
var a = new Object(); console.log(typeof a); //输出 object
var a = Object; console.log(typeof a) //输出 function
var getType=Object.prototype.toString;
getType.call('aaa');//输出 [object String]
getType.call(2222);//输出 [object Number]
getType.call(true);//输出 [object Boolean]
getType.call(undefined);//输出 [object Undefined]
getType.call(null);//输出 [object Null]
getType.call({});//输出 [object Object]
getType.call([]);//输出 [object Array]
getType.call(function(){});//输出 [object Function]
w3c:JavaScript 数据类型
脚本之家:js 判断各种数据类型的方法
阮一峰:Javascript标准参考
感谢大家观看
BY : 黄麒二|陈中彬