js数据类型以及判断数据类型的方法

目录

一.JS数据类型:

二、JS 中判断数据类型的方法

 1、使用 typeof

为什么使用typefo检测Null为object类型?

2、使用 instanceof 

3、使用Object.prototype.toString.call

4、使用constructor


一.JS数据类型:

        EAMCscript分为:基本数据类型,复杂数据类型

        基本数据类型有:         

数据类型 说明
数值类型Number 只能是整数或者小数
字符串类型String 字符串类型string,用单引号或者双引号 包裹的任何字符
布尔类型Boolean 只能是true或false代表真假
未定义Undefined 定义变量后不进行赋值 这个变量就是Undefined
空null是对象类型 null值表示一个空对象指针

       

        复杂数据类型:

数据类型 说明
对象类型object 数组对象、数学对象、日期对象、正则对象(RegExp)等等

二、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可以测试出numberstringbooleanundefinedfunction

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

为什么使用typefo检测Null为object类型?

在JS中进行数据底层存储的时候是用二进制存储的,这是一定的,而且它的前三位是代表存储的数据类型,而000是代表object类型也就是引用类型的数据。而null正好全是0,所以它巧妙的符合object类型的存储格式,所以在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的指向是可以改变的

参考地址:JS 判断数据类型的方法_s_kzn的博客-CSDN博客_js判断数据类型的方法

参考地址:为什么typeof null是object?_CreatorRay的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/Tie_zhu/article/details/127441415