typeof 判断6种
1 //typeof 6种 2 console.log(typeof 1); //number 3 console.log(typeof ‘‘); //string 4 console.log(typeof true); //boolean 5 console.log(typeof {}); //object 6 console.log(typeof fn); //function 7 console.log(typeof undefined); //undefined
方法:
1 function isType(value){//不支持IE6 2 var str = Object.prototype.toString.call(value); 3 return str.slice(8,str.length-1).toLowerCase(); 4 }
这个比较全
1 var data = new Date(); 2 function fn(){ 3 alert(1) 4 } 5 6 console.log(isType(1)); //number 7 console.log(isType("1")); //string 8 console.log(isType(true)); //boolean 9 console.log(isType(fn)); //function 10 console.log(isType(null)); //null 11 console.log(isType(undefined)); //undefined 12 console.log(isType({})); //object 13 console.log(isType([])); //array 14 console.log(isType(/12/)); //regexp 15 console.log(isType(data)); //date
时间: 2024-10-14 03:06:46