JS 判断数据类型的方法

一、JS 中的数据类型
ECMAScript中有5中简单数据类型(也称为基本数据类型): Undefined、Null、Boolean、Number和String。还有1中复杂的数据类型————Object,Object本质上是由一组无序的名值对组成的。

其中Undefined、Null、Boolean、Number都属于基本类型。

Object、Array和Function则属于引用类型,


二、JS 中判断数据类型的方法
1、使用 typeof
let obj={
            name:'dawn',
            age:21
        }
        let fn=function() {
            console.log('我是 function 类型');
        }
        console.log(typeof 1);       //number
        console.log(typeof 'abc');   //string
        console.log(typeof true);    //boolean
        console.log(typeof undefined);  //undefined 
        console.log(typeof fn);      //function
        console.log(typeof (new Date) );  //object
        console.log(typeof null);     //object
        console.log(typeof [1,2,3]);  //object
        console.log(typeof obj);      //object
由结果可知typeof可以测试出number、string、boolean、undefined及function,

【注意】:对于null及数组、对象,typeof均检测出为object,不能进一步判断它们的类型。

2、使用 instanceof 
obj instanceof Object ,可以左边放你要判断的内容,右边放类型来进行JS类型判断,只能用来判断复杂数据类型,因为instanceof 是用于检测构造函数(右边)的 prototype 属性是否出现在某个实例对象(左边)的原型链上。

 let arr=[1,2,3,4,5,6,7]
        let obj={
            name:'dawn',
            age:21
        }
        let fn=function() {
            console.log('我是 function 类型');
        }
 
       console.log(arr instanceof Array);  //true
       console.log(obj instanceof Object);  //true
       console.log(fn instanceof Function);  //true
       console.log((new Date) instanceof Date);  //true
3、使用Object.prototype.toString.call
let obj = {
            name: 'dawn',
            age: 21
        }
        let fn = function () {
            console.log('我是 function 类型');
        }
        
        console.log(Object.prototype.toString.call(1));        // [object Number]
        console.log(Object.prototype.toString.call('Hello tomorrow')); // [object String ]
        console.log(Object.prototype.toString.call(true));     // [object Boolean]
        console.log(Object.prototype.toString.call(undefined));  // [object Undefined]
        console.log(Object.prototype.toString.call(fn));   // [object Function]
        console.log(Object.prototype.toString.call(new Date));  // [object Date]
        console.log(Object.prototype.toString.call(null));   // [object Null]
        console.log(Object.prototype.toString.call([1, 2, 3]));  // [object Array]
        console.log(Object.prototype.toString.call(obj));       // [object Object]
在任何值上调用 Object 原生的 toString() 方法,都会返回一个 [object NativeConstructorName] 格式的字符串。每个类在内部都有一个 [[Class]] 属性,这个属性中就指定了上述字符串中的构造函数名。
但是它不能检测非原生构造函数的构造函数名。

4、使用constructor
let arr = [1, 2, 3, 4, 5, 6, 7]
        let obj = {
            name: 'dawn',
            age: 21
        }
        let fn = function () {
            console.log('我是 function 类型');
        }
 
        console.log((9).constructor === Number);  //true
        console.log('hello'.constructor === String);  //true
        console.log(true.constructor === Boolean);  //true
        console.log(fn.constructor === Function);  //true
        console.log((new Date).constructor === Date);  //true
        console.log(obj.constructor === Object);  //true
        console.log([1, 2, 3].constructor === Array);  //true
 【注意】:constructor不能判断undefined和null,并且使用它是不安全的,因为contructor的指向是可以改变的

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值