数据类型检测的方式有哪些

1、typeof

  • typeof在对值类型numberstringbooleansymbol、 undefined、 function、 bigint的反应是精准的;

  • 但对于对象{ } 、数组[ ] null 都会返回 object

    console.log(typeof "");            // string
    console.log(typeof 1);             // number
    console.log(typeof NaN);           // number
    console.log(typeof true);          // boolean
    console.log(typeof Symbol(1))      // "symbol"
    console.log(typeof BigInt(11))     // "bigint"
    console.log(typeof undefined);     // undefined
    console.log(typeof function(){});  // function
    
    console.log(typeof null);   // object   JavaScript语言设计上的一个历史遗留问题
    console.log(typeof []);     // object  // 引用类型
    console.log(typeof {});     //object  // 引用类型

 2、instanceof

instanceof可以正确判断对象的类型,其内部运行机制是判断在其原型链中能否找到该类型的原型

  • instanceof 判断一个实例是否属于某种类型

  • instanceof 运算符只能正确判断引用数据类型,而不能判断基本数据类型

  • instanceof可以用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性

    // 检测构造函数B的原型是否有出现在对象A的原型链上。
    A instanceof B 
    
    [] instanceof Array // true
    [].__proto__ == Array.prototype // true
    // 检测基础数据类型
    console.log("1" instanceof String); // false
    console.log(1 instanceof Number); // false
    console.log(NaN instanceof Number); // false
    console.log(true instanceof Boolean); // false
    

    基本类型封装成对象后才返回true

    console.log(new String("1") instanceof String); // true
    console.log(new Number(1) instanceof Number); // true
    console.log(new Number(NaN) instanceof Number); // true
    console.log(new Boolean(true) instanceof Boolean); // true

3、constructor 

constructor 是每个实例对象都拥有的属性

constructor有两个作用:

(1)判断数据的类型;

(2)对象实例通过 constrcutor 对象访问它的构造函数(实例的constrcutor属性,指向构造函数本身);

function Hello() {}; // 构造函数
var h = new Hello(); // 实例化对象

console.log(Hello.prototype.constructor == Hello); // true
console.log(h.constructor == Hello); // true ()
console.log(("1").constructor === String);              // true
console.log((1).constructor === Number);                // true
console.log((NaN).constructor === Number);              // true
console.log((true).constructor === Boolean);            // true
console.log(([]).constructor === Array);                // true
console.log((function () {}).constructor === Function); // true
console.log(({}).constructor === Object);               // true
console.log((Symbol(1)).constructor === Symbol);        // true

console.log((null).constructor === Null);               // 报错
console.log((undefined).constructor === Undefined);     // 报错

4、Object.prototype.toString.call()

Object.prototype.toString.call() 使用 Object 对象的原型方法toString 来判断数据类型

返回的结果结构是[object 数据类型],因此简单封装

// 定义检测数据类型的功能函数
function checkedType(target) {
	return Object.prototype.toString.call(target).slice(8, -1);
}

console.log(checkedType(1));              // Number
console.log(checkedType("1"));            // String
console.log(checkedType(NaN));            // Number
console.log(checkedType(true));           // Boolean
console.log(checkedType(Symbol(1)));      // Symbol
console.log(checkedType(null));           // Null
console.log(checkedType(undefined));      // Undefined
console.log(checkedType([]));             // Array
console.log(checkedType({}));             // Object
console.log(checkedType(function () {})); // Function

5、其它补充

通过ES6的Array.isArray()来判断数组

Array.isArrray(obj);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值