JavaScript判断数据类型的方式

JavaScript判断数据类型的方式

js中的数据类型有哪些?

  • 基本数据类型:number、string、boolean、null、undefined、symbol以及未来ES10新增的BigInt(任意精度调整)。
  • 引用数据类型:对象Object、数组Array、函数Function

判断数据类型的方法有哪些?

  1. 最常见的判断方法:typeof
  2. 已知对象类型:instanceof
  3. 对象原型链判断方法:Object.prototype.toString.call()
  4. 根据对象的构造器constructor进行判断
  5. jQuery方法:jQuery.type()
  6. 严格运算符:===

一、typeof

注:typeof返回的类型都是字符串形式。
返回六个字符串:string, boolean, number, function, object, undefined

typeof null // object
typeof function(){
    
    } // function
typeof new Function() // function
// 基本数据类型判断
typeof 1 // 'number'
typeof 'a' // 'string'
typeof false // 'boolean'
typeof undefined // 'undefined'
typeof Symbol() // 'symbol'

// 引用数据类型判断和new关键字创建的变量判断类型
typeof {
    
    } // 'object'
typeof [] // 'object'
typeof new Number(1) // 'object'
typeof new String('a') // 'object'
typeof new Boolean(true) // 'object'
typeof new Number(1) // 'object'
typeof new Date() // 'object'
function Car () {
    
    } // 定义一个构造函数
typeof new Car() // 'object'

我们可以发现,typeof在判断null、array、object以及函数实例(new + 函数)时,得到的都是object,这使得在判断这些数据类型的时候,得不到真正的结果,由此引出instanceof。

二、instanceof

注:instanceof后面一定要是对象类型,并且大小写不能写错。

 function Car () {
    
    } // 定义一个构造函数
 console.log(new Car() instanceof Car) // true
 console.log(new Car() instanceof Function) // false
 console.log(new Car() instanceof Object) // true
 console.log({
    
    } instanceof Object) // true
 console.log([] instanceof Array) // true
 console.log(new String('') instanceof String) // true
 console.log('' instanceof String) // false

三、对象原型链判断方法:Object.prototype.toString.call()

Object.prototype.toString.call(null) // [object Null]
Object.prototype.toString.call(undefined) // [object Undefined]
Object.prototype.toString.call(1) // [object Number]
Object.prototype.toString.call(new Number(1)) // [object Number]
Object.prototype.toString.call('a') // [object String]
Object.prototype.toString.call(new String('a')) // [object String]

Object.prototype.toString.call({
    
    }) // [object Object]
Object.prototype.toString.call([]) // [object Array]
Object.prototype.toString.call(new Date()) // [object Date]
Object.prototype.toString.call(/^d$/g) // [object RegExp]
Object.prototype.toString.call(() => {
    
    }) // [object Function]
Object.prototype.toString.call(new Function()) // [object Function]

function Car () {
    
    } // 定义一个构造函数
Object.prototype.toString.call(new Car()) // [object Object]

toString方法,在Object原型上返回数据格式。

四、根据对象的constructor进行判断

var a = "iamstring.";
var b = 222;
var c= [1,2,3];
var d = new Date();
var e = function(){
    
    alert(111);};
var f = function(){
    
    this.name="22";};  
c.constructor === Array // true
d.constructor === Date // true
e.constructor === Function // true

constructor判断方法跟instanceof相似,但是constructor检测Object与instanceof不一样,constructor还可以处理基本数据类型的检测,不仅仅是对象类型。

注:null和undefined没有constructor
判断数字时使用(),例如:(123).constructor
constructor在类继承时会出错,因为Object被覆盖掉了,检测结果会出错。

在这里插入图片描述

五、jQuery.type()

如果对象是null或undefined,则直接返回“null”和“undefined”。

注:在使用时,一定要引入jQuery文件,不然会报错,jQuery is not defined

console.log(jQuery.type(undefined) === "undefined") // true
console.log(jQuery.type() === "undefined") // true
console.log(jQuery.type(window.notDefined) === "undefined") // true
console.log(jQuery.type(152) === "number") // true
console.log(jQuery.type('188') === "string") // true
console.log(jQuery.type([]) === "array") // true
console.log(jQuery.type(true) === "boolean") // true
console.log(jQuery.type(function(){
    
    }) === "function") // true
console.log(jQuery.type(new Date()) === "date") // true
console.log(jQuery.type(/\d/) === "regexp") // true
console.log(jQuery.type(new Error()) === "error") // true  jquery版本高于1.9.3
console.log(jQuery.type({
    
    name:'Hello'}) === "object") // true
console.log(jQuery.type(Symbol()) === "symbol") // true

六、严格运算符:===

通常用于判断一个变量是否为空,变量是否为数据等。

var a = null;
typeof a; // 'object'
a === null; // true

猜你喜欢

转载自blog.csdn.net/weixin_45832482/article/details/124101145