检测数据类型方式?typeof, instanceof, Array.isArray(数组名称), Object.prototype.toString.call();

  1. typeof 用于检测undefined, null, number, boolean, function。
    无法判断:复杂类型,比如:对象、数组、正则。
    写法:
    1.typeof () :括号中为判断的变量;
    2.typeof 判断的变量。
    返回值: 字符串。(返回值为字符串)
        console.log(typeof (undefined)); // undefine
        console.log(typeof (null)); // object
        console.log(typeof (false)); // boolean
        console.log(typeof ('123')); // string
        console.log(typeof (123)); // number
        console.log(typeof (NaN)); // number
        console.log(typeof (function func() {})); // function
        console.log(typeof (class func1 {})); // function
        let obj = {
            name: 'cc',
            age: 18
        };
        console.log(typeof (obj)); //  object
        let arr = [1, '111', 'string'];
        console.log(typeof (arr)); // object
  1. instanceof 用于判断复杂类型。
    写法: 判断的变量 instanceof Object/Array
    返回值: 布尔值(true/false)。
		 let obj = {
            name: 'cc',
            age: 18
        };
        console.log(obj instanceof Object); // true
         let arr = [1, '111', 'string'];
        console.log(arr instanceof Array); // true
  1. Array.isArray()用于判断数组。
    写法: Array.isArray(判断的变量)
    返回值: 布尔值(true, false);
 let arr = [1, '111', 'string'];
console.log(Array.isArray(arr)); // true
 let obj = {
            name: 'cc',
            age: 18
};
console.log(Array.isArray(obj)); // false
  1. Object.prorotype.toString.call();
    写法: Object.prototype.toString.call(判断的数据)
		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(false)); // [object Boolean]
        console.log(Object.prototype.toString.call(null)); // [object Null]
        console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
        console.log(Object.prototype.toString.call({})); // [object Object]
        console.log(Object.prototype.toString.call(obj)); // [object Object]
        console.log(Object.prototype.toString.call([])); // [object Array]
        console.log(Object.prototype.toString.call(arr)); // [object Array]
        console.log(Object.prototype.toString.call(new Date())); // [object Date]
        console.log(Object.prototype.toString.call(function () {})); // [object Function]
        console.log(Object.prototype.toString.call(class {})); // [object Function]
        console.log(Object.prototype.toString.call(/d/g)); // [object RegExp]

        function Person() {}
        console.log(Object.prototype.toString.call(new Person)); // [object Object]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值