javascript 不同数据类型判断

js 数据类型检测

在开始接触js时,我们会先了解js的类型。js中有以下几种类型:

  • 主要(基本)数据类型是:
    字符串 —— string
    数值 —— number
    布尔 —— boolean
  • 复合(引用)数据类型是:
    对象 —— object
    数组 —— array
  • 特殊数据类型是:
    Null
    Undefined

对于以上几种类型,用什么方式来进行检测呢?

一、typof
	var obj = {};
	function fun(){};
	var arr = [];
	
    console.log(typeof "abc");  //string
    console.log(typeof 123);  //number
    console.log(typeof false);  //boolean
	
	console.log(typeof obj);  //object
    console.log(typeof fun);  //function
    console.log(typeof arr);  //object
	
	console.log(typeof null);  //object

返回值类型:string
总结:一般只用来检测基本数据类型。函数会返回function,数组和对象会返回object。null在js中表示一个空对象,故,null会返回object

二、instanceof
	var obj = {};
	function fun(){};
	var arr = [];
	
	console.log("string" instanceof String);  //false
    console.log(123 instanceof Number);  //false
    console.log(false instanceof Boolean);  //false
    
    console.log(obj instanceof Object);  //true
    console.log(obj instanceof Function);  //false
    
    console.log(fun instanceof Object);  //true
    console.log(fun instanceof Function);  //true
    
    console.log(arr instanceof Object);  //true
    console.log(arr instanceof Array);  //true
	
	console.log(null instanceof Object);  //false

返回值类型:boolean
总结:一般用来检测object,无法正确检测基本数据类型。
function和array都是object类型,使用instanceof Object也会返回true。

三、constructor
	var obj = {};
	function fun(){};
	var arr = [];
	
	console.log(obj.constructor == Object);  //true
    
    console.log(fun.constructor == Object);  //false
    console.log(fun.constructor == Function);  //true
    console.log(fun.constructor === Function);  //true
    console.log("fun".constructor == String);  //true
    
	console.log(arr.constructor == Object);  //false
    console.log(arr.constructor == Array);  //true
    console.log(arr.constructor === Array);  //true
    
    console.log(false.constructor == Boolean);  //true

返回值类型:boolean
总结:constructor(构造函数)检测 能正确判断数据类型,而且通过constructor和prototype(原型)也能用来判断两个对象是否相等。

四、Object.prototype.toString.call() 或者 Object.prototype.toString.apply()
	var obj = {};
	function fun(){};
	var arr = [];
	
	console.log(Object.prototype.toString.call("abc"));  //[object String]
    console.log(Object.prototype.toString.call(123));  //[object Number]
    console.log(Object.prototype.toString.call(false));  // [object Boolean]
        
    console.log(Object.prototype.toString.call(obj));  //[object Object]
    console.log(Object.prototype.toString.call(fun));  //[object Function]
    console.log(Object.prototype.toString.call(arr));  //[object Array]

	console.log(Object.prototype.toString.call(null));  //[object Null]
    console.log(Object.prototype.toString.call(undefined));  //[object Undefined]

上面例子将 call 换成 apply 得到的结果是一样的;
返回值类型:string
总结:返回的结果都是object类型,可以坐结果中判断出是哪种类型。

对于上面的几种 js 类型检测 方式,可根据应用场景选择使用,能正确判断数据类型即是我们想要的结果!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值