判断数据类型的几种方法

1、typeof,返回的都是字符串

typeof判断函数返回的是function,判断字符串返回的是string,判断对象、数组、null均返回object。

  • typeof 可以判断function的类型,在判断除Object类型的对象时比较方便。
console.log(typeof 1)   // "number"
console.log(typeof 'dfjidf')  //  "string" 
console.log(typeof true)   //  "boolean"
console.log(typeof undefined)  //  "undefined"
console.log(typeof window.hhhhhhh)  //  "undefined"

console.log(typeof function(){}) // "function"
console.log(typeof null)  // "object"
console.log(typeof [])  //   "object"
console.log(typeof {})  //  "object"
console.log(typeof new Date())  //  "object"
console.log(typeof new RegExp())  // "object"
console.log(typeof new Error())  //  "object"
  • typeof和length结合可以判断数组和对象,但是当对象有一个length属性并且值为number时该方法则失效。
console.log(typeof []) 
console.log(typeof [].length)   // "number"
console.log(typeof {})
console.log(typeof {}.length)  // "undefined"
  • typeof和length结合判断数组和对象无效的情况:
console.log(typeof []) 
console.log(typeof [].length)   // "number"
console.log(typeof {})
console.log(typeof {length:0}.length)  // "number"
2、如果已知对象类型,可以使用: instanceof
  • 数组是Array的实例,也是Object的实例。
  • null不是Object的实例。
  • 直接通过字面量定义的字符串不是String的实例,通过String构造函数得到的字符串是String的实例。
  • 注意:instanceof 后面一定要是对象类型(首字母大写),并且大小写不能写错,否则报错。
  • instanceof方法无论对象是直接继承还是间接继承的都会报true。
console.log( {} instanceof Object)   // true 

console.log( [] instanceof Object)   // true
console.log( [] instanceof Array)   // true

console.log( 'fuifh' instanceof String)   // false
console.log( new String('fuifh') instanceof String)   // true

console.log( null instanceof Object) // false

console.log( function () {} instanceof Function)  // true  
console.log( new RegExp() instanceof RegExp)  // true
console.log( new Date() instanceof Date)  // true


// instanceof 判断直接继承和间接继承的对象都是返回true
function Person () {
  
}
function Man () {
   
}

Man.prototype = new Person   // Man继承自Person
var p = new Man ()

console.log(p.constructor === Man)   // false
console.log(p.constructor === Person)  // true
console.log(p instanceof Man)  // true
console.log(p instanceof Person)  // true
3、根据对象的constructor判断: constructor
  • null没有constructor属性
console.log( {}.constructor === Object)  // true
console.log( [].constructor === Object)  // false
console.log( [].constructor === Array)   // true
console.log( 'fuifh'.constructor === String)  // true
console.log( (new String('fuifh')).constructor === String)  // true   
console.log( (function () {}).constructor === Function)  // true 
console.log( (new RegExp()).constructor === RegExp)  // true
console.log( (new Date()).constructor === Date)  // true
console.log( null.constructor === Object)  // 报错,null没有constructor属性
  • 注意: constructor 在类继承时会出错
function Person () {
  
}
function Man () {
   
}
var p = new Man ()

console.log(p.constructor === Man)   // true
console.log(p.constructor === Person)  // false
console.log(p instanceof Man)  // true
console.log(p instanceof Person)  // false
function Person () {
  
}
function Man () {
   
}

Man.prototype = new Person   // Man继承自Person
var p = new Man ()

console.log(p.constructor === Man)   // false
console.log(p.constructor === Person)  // true
console.log(p instanceof Man)  // true
console.log(p instanceof Person)  // true

解决construtor的问题通常是让对象的constructor手动指向自己:

function Person () {
  
}
function Man () {
   
}

Man.prototype = new Person
var p = new Man ()
p.constructor = Man   // 手动指向Man,将自己的类赋值给对象的constructor属性

console.log(p.constructor === Man)  //true  
console.log(p.constructor === Person)   //false
console.log(p instanceof Man)   // true
console.log(p instanceof Person)   // true
4、 prototype和call/apply结合使用,例子中的call可以替换为apply
console.log(Object.prototype.toString.call('fhakfk') === "[object String]")   // true
console.log(Object.prototype.toString.call([1,432,564]) === "[object Array]")   // true
console.log(Object.prototype.toString.call({}) === "[object Object]")   // true
console.log(Object.prototype.toString.call(undefined))   //  "[object Undefined]"
console.log(Object.prototype.toString.call(null))   //  "[object Null]"
console.log(Object.prototype.toString.call(new Date()))  // "[object Date]" 
console.log(Object.prototype.toString.call(function () {}))  //  "[object Function]"
console.log(Object.prototype.toString.call(true))  //  "[object Boolean]"
5、jQuery.type()或$.type()
  • undefined或null,则返回相应的“undefined”或“null”。
jQuery.type( null ) === "null"
jQuery.type( undefined ) === "undefined"
jQuery.type() === "undefined"
jQuery.type( window.hhhhhh) === "undefined"
jQuery.type( true ) === "boolean"
jQuery.type( 3 ) === "number"
jQuery.type( "jfdsfi" ) === "string"
jQuery.type( function(){} ) === "function"
jQuery.type( [] ) === "array"
jQuery.type( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"
  • 其他一切都将返回它的类型“object”。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值