判断变量的类型(typeof、instanceof、Object.prototype.toString)
2017-09-06 14:23
477 查看
1、typeof
返回一个变量的基本类型,用来检测值类型的数据类型,只有以下几种:
2、instanceof
返回的是一个布尔值,用来判断对象和函数,不能用来判断字符串和数字等
3、Object.prototype.toString
用来精确判断对象的类型
返回一个变量的基本类型,用来检测值类型的数据类型,只有以下几种:
number,boolean,string,object,undefined,function alert(typeof 1) //number alert(typeof "1") //string alert(typeof []) //object alert(typeof null) //object alert(typeof a) //undefined
2、instanceof
返回的是一个布尔值,用来判断对象和函数,不能用来判断字符串和数字等
var a={}; alert(a instanceof Object); //true var b=[]; alert(b instanceof Array); //true alert("string" instanceof String); //false alert(new String("string")instanceof String); //true
3、Object.prototype.toString
用来精确判断对象的类型
alert(Object.prototype.toString.call([])) // "[object Array]" Object.prototype.toString.call({}) //"[object Object]" Object.prototype.toString.call(new Date()) //"[object Date]"
相关文章推荐
- jquery判断变量类型typeof和Object.prototype.toString.apply()
- js判断对象类型 typeof与Object.prototype.toString.call方法的区别
- 判断一个变量的类型Object.prototype.toString.call
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- typeof,instanceof,Object.prototype.toString来判断数据类型
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- 检测变量类型之typeof,instanceof,Object.prototype.toString
- typeof 和 Object.prototype.toString.call 数据类型判断的区别
- instanceof和typeof都能用来判断一个变量是否为空或是什么类型的变量。
- Object.prototype.toString.call() 区分对象类型(判断对象类型)
- Object.prototype.toString.call() 进行类型判断
- object.prototype.toString()判断类型
- Object.prototype.toString vs typeof
- Object.prototype.toString.call()进行类型判断
- Object.prototype.toString & typeof
- 判断js中各种数据的类型方法之 typeof() 和 Object.prototype.toString() 详解
- Object.prototype.toString.call()进行类型判断
- 通过 Object.prototype.toString.call() 进行类型判断
- 如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较
- js中通过Object.prototype.toString方法----精确判断对象的类型