js判断数据类型的方法

js判断数据类型的方法(常见的六种)

1.最常见的判断方法:typeof

2.已知对象类型: instanceof

3.对象原型链判断方法: prototype 通用但很繁琐

4.根据对象的构造器constructor进行判断

5.jQuery方法: jquery.type()

6.严格运算符: ===

一、typeof

 console.log(typeof "helloworld");
    console.log(typeof 12);
    console.log(typeof [1, 2, 3]);
    console.log(typeof new Function());
    console.log(typeof new Date());
    console.log(typeof new RegExp());
    console.log(typeof Symbol());
    console.log(typeof true);
    console.log(typeof null);
    console.log(typeof undefined);

运行结果

二.instance of

判断是否为object(对象类型)基本数据类型不是,引用数据类型是

console.log(phone instanceof Object);
    console.log(document.getElementsByTagName('div')[0] instanceof Object);
    console.log(window instanceof Object);
    var obj = {}
    console.log(obj instanceof Object);
    console.log(new Date() instanceof Object);
    var arr = []
    console.log(arr instanceof Object);
    var fun = function () { }
    console.log(fun instanceof Object);
    var num = 10
    var str = '哈哈'
    console.log(num instanceof Object);
    console.log(str instanceof Object);
    console.log(null instanceof Object);
    console.log(undefined instanceof Object);

运行结果

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

适用于所有类型的判断检测,注意区分大小写. toString方法,在Object原型上返回数据格式,

 console.log(Object.prototype.toString.call("123"))
    console.log(Object.prototype.toString.call(123))
    console.log(Object.prototype.toString.call(true))
    console.log(Object.prototype.toString.call([1, 2, 3]))
    console.log(Object.prototype.toString.call(null))
    console.log(Object.prototype.toString.call(undefined))
    console.log(Object.prototype.toString.call({ name: 'Hello' }))
    console.log(Object.prototype.toString.call(function () { }))
    console.log(Object.prototype.toString.call(new Date()))
    console.log(Object.prototype.toString.call(/\d/))
    console.log(Object.prototype.toString.call(Symbol()))

运行结果

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

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

1.null和undefined没有constructor;
2.判断数字时使用(),比如 (123).constructor,如果写成123.constructor会报错
3.constructor在类继承时会出错,因为Object被覆盖掉了,检测结果就不对了

 // 1、DOM对象
    console.log(document.getElementsByTagName('div')[0].constructor); // HTMLDivElement() { [native code] }
    // 2、BOM对象
    console.log(history.constructor); //  History() { [native code] }
    // 3、自定义对象
    var obj = {}
    console.log(obj.constructor); // Object() { [native code] }
    // 4、内置对象
    console.log(new Date().constructor); // Date() { [native code] }
    // 5、数组
    var arr = []
    console.log(arr.constructor); // Array() { [native code] }
    // 6、函数
    var fun = function () {

    }
    // 判断数据类型  constructor
    console.log(fun.constructor); // Function() { [native code] }
    var num = 10
    console.log(num.constructor == Object); // false

运行结果

五、jQuery方法: 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(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

六.有局限的判断:严格运算符===

通常===出现在我们的条件判断中,比如判断一个变量是否为空,变量是否为数据等,示例如下

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

    /*扩展补充*/

    //判断一个非数组变量是否为空
    if (typeof a != 'undefined' && a) { }

    //判断一个数组变量是否为空
    if (typeof a != 'undefined' && a && a.length > 0) { }

总之:

一般变量用typeof,

已知对象类型用instanceof,

通用方法Object.prototype.toString.call()

jQuery项目万能方法jQuery.type()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值