JS数据类型之5种判断方式?

1.JS的数据类型有哪些呢?

(1).基本数据类型(又称类型值)如下7种:

 Number(数值)、String(字符串)、Boolean(布尔值)、Undefined(未定义)、Null(空)、Symbol(es6新增,表示独一无二的值) 和 BigInt(es10新增)

 BigInt 支持比Number更大的整数,可以解决整数溢出的问题,用法如下:

BigInt(9007199254740995);    // → 9007199254740995n

(2).引用数据类型:如以下5种:

Object(对象)、Array(数组)、 function(函数)、Date(日期)、RegExp(正则)。 

 2.JS判断数据类型的方式有哪些?

 (1).typeof  (注意这里返回的数据类型都是字符串,后三种为引用数据类型检测不出来

<script>
    console.log(typeof "helloworld") //-- -- -- -- -- -- -- -- -- > "string"
    console.log(typeof 123) // -- -- -- -- -- -- -- -- -- > "number"
    console.log(typeof [1, 2, 3]) //-- -- -- -- -- -- -- -- -- > "object"
    console.log(typeof new Function()) //-- -- -- -- -- -- -- -- -- > "function"
    console.log(typeof new Date()) //-- -- -- -- -- -- -- -- -- > "object"
    console.log(typeof new RegExp()) // -- -- -- -- -- -- -- -- -- > "object"
    console.log(typeof Symbol()) //-- -- -- -- -- -- -- -- -- > "symbol"
    console.log(typeof true) //-- -- -- -- -- -- -- -- -- > "Boolean"
    console.log(typeof null) //-- -- -- -- -- -- -- -- -- > "object"
    console.log(typeof undefined) //-- -- -- -- -- -- -- -- -- > "undefined"
    console.log(typeof 'undefined') //-- -- -- -- -- -- -- -- -- > "string"
    console.log(Object); //-- -- -- -- -- -- -- -- -- > ƒ Object() { [native code] }
    console.log(Array); //-- -- -- -- -- -- -- -- -- > ƒ Array() { [native code] }
    console.log(Function); //-- -- -- -- -- -- -- -- -- > ƒ Function() { [native code] }
</script>

(2) 、instanceof(用于检测复杂数据类型,检测简单数据类型就会报错,检测的数据类型不匹配就会返回false)

    console.log([] instanceof Array); //-- -- -- -- -- -- -- -- -- >true
    console.log({}
        instanceof Object); //-- -- -- -- -- -- -- -- -- >true
    console.log(Function() instanceof Function); //-- -- -- -- -- -- -- -- -- >true
    console.log(new Date() instanceof Date);
    console.log(null instanceof Object); //-- -- -- -- > false
    console.log(true instanceof true); //-- -- -- -- > 报错

 (3)、对象原型链方式,Object.prototype.toString.call,适用于所有数据类型的检测,注意O大写,S大写。

    console.log(Object.prototype.toString.call("123")) //-- -- -- -- > [object String]
    console.log(Object.prototype.toString.call(123)) //-- -- -- -- > [object Number]
    console.log(Object.prototype.toString.call(true)) // -- -- -- -- > [object Boolean]
    console.log(Object.prototype.toString.call([1, 2, 3])) //-- -- -- -- > [object Array]
    console.log(Object.prototype.toString.call(null)) // -- -- -- -- > [object Null]
    console.log(Object.prototype.toString.call(undefined)) //-- -- -- -- > [object Undefined]
    console.log(Object.prototype.toString.call({
            name: 'Hello'
        })) //-- -- -- -- > [object Object]
    console.log(Object.prototype.toString.call(function() {})) //-- -- -- -- > [object Function]
    console.log(Object.prototype.toString.call(new Date())) // -- -- -- -- > [object Date]
    console.log(Object.prototype.toString.call(/\d/)) //-- -- -- -- > [object RegExp]
    console.log(Object.prototype.toString.call(Symbol())) //-- -- -- -- > [object Symbol]

 (4).constructor 可以检测所有数据类型(除了null和undefined)。

    //用constructor判断基本数据类型
    console.log((123).constructor === Number) // true
    console.log(('string').constructor === String) //true
    console.log([].constructor === Array);
    console.log(null.constructor === null); // 报错
    console.log(undefined.constructor === undefined); //报错
    console.log((123).constructor === Symbol); // false  代它不是一个单一的数
    console.log(BigInt(9999999999).constructor === BigInt); //true
    //用constructor判断复杂数据类型
    console.log([].constructor === Array); //-- -- -- -- -- -- -- -- -- >true
    console.log({}.constructor === Object); //-- -- -- -- -- -- -- -- -- >true
    console.log(Function().constructor === Function); //-- -- -- -- -- -- -- >true
    console.log(new Date().constructor === Date); // true

但是constructor有一个缺点就是判断类继承时会报错,具体看如下代码:

    //注意当出现继承的时候,使用constructor会出现问题
    function A() {};

    function B() {};
    console.log(A.prototype); //{constructor: ƒ}
    console.log(new B()); //原型中有一个{constructor: ƒ}
    A.prototype = new B(); //A继承自B
    console.log(A.prototype); //A的prototype指向了B {}
    console.log(A.constructor); //ƒ Function() { [native code] }自身的构造函数
    console.log(B); //ƒ B() {}//自身的实例对象
    console.log(A.constructor === B) //所以-- -- -- -- > false
    var C = new A();
    console.log(new A()); //因为A的原型prototype指向了B {}
    console.log(C.constructor === B) //所以-- -- -- -- > true
    console.log(C.constructor === A) //所以 -- -- -- -- > false
        //这个时候就需要用到手动来修改constructor指向
    C.constructor = A; //将自己的类赋值给对象的constructor属性
    console.log(C.constructor); //ƒ A() {}
    console.log(A); //ƒ A() {}
    console.log(C.constructor === A);
    //所以-- -- -- -- > true
    console.log(C.constructor === B);
    //所以-- -- -- -- > false

5.jquery.type,这个方法无脑判断就是了:可以检测任何数据类型


    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(123) === "number") //-- -- -- -- > true
    console.log(jQuery.type('123') === "string") //-- -- -- -- > true
    console.log(jQuery.type([]) === "array") // -- -- -- -- > true
    console.log(jQuery.type(true) === "boolean") // -- -- -- -- > true
    console.log(jQuery.type(function() {}) === "function") //-- -- -- -- > rue
    console.log(jQuery.type(new Date()) === "date") // -- -- -- -- > true
    console.log(jQuery.type(/\d/) === "regexp") // -- -- -- -- > true
    console.log(jQuery.type(new Error()) === "error") //-- -- -- -- > true jq版本高于1 .9 .3
    console.log(jQuery.type({
            name: 'Hello'
        }) === "object") //-- -- -- - > true
    console.log(jQuery.type(Symbol()) === "symbol") //-- -- -- - > true
        //-- -- -- - > 其余对象类型一般返回object

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值