js判断数据类型的三种方式

判断js的类型的方法有typeof; Object.prototype.toString.call;instanceof

(一)js的基本类型有number;string;boolean;null;undefined;Symbol;引用类型有object;function;array, Date;Math等

(二)判断js的类型

1.typeof 判断数据类型

用法:typeof  data

基本类型除了null,其余都返回对应的字符串类型


    console.log(typeof 1);  //number
    console.log(typeof "你好");  //string
    console.log(typeof true);  //boolean
    console.log(typeof null);  //object
    console.log(typeof undefined);  //undefined
    console.log(typeof Symbol());  //symbol

 引用类型除了fucntion,其余都返回obejct

console.log(typeof []); //object
console.log(typeof { name: "树先生" }) //object
console.log(typeof new Date()) //obejct
console.log(typeof (() => { return 2 })) //function

2.instanceof 检测构造函数的prototype是否存在对象的原型链上面

用法:object  instanceof constructor

所有对象的最高级原型都是object

    // 构造函数A,B
    function A() {};

    function B() {};

    var c = new A(); //实例化c
    //c在A的原型链上面
    console.log(c instanceof A, c.__proto__, A.prototype); //true
    //c不在B的原型链上面
    console.log(c instanceof B); //false

    A.prototype = {}; //改变A的原型链

    var d = new A(); //实例化d
    //d在A的原型链上面
    console.log(d instanceof A, d.__proto__, A.prototype); //true
    //c不在A的原型链上面,因为A的原型指向了空对象
    console.log(c instanceof A); //false

    // 继承
    B.prototype = new A(); //B继承A的原型

    var e = new B();
    //e在B的原型链上面
    console.log(e instanceof B, B.prototype); //true
    //e在A的原型链上面;因为B、继承了A的原型
    console.log(e instanceof A); //true
    //e的原型和c的原型不是同一个原型;因为e继承的是改变后的A的原型
    console.log(e.__proto__ === c.__proto__, c.__proto__); //false
    //e在的原型链上面
    console.log(e.__proto__ === B.prototype); //true
    //B继承了A的实例的原型
    console.log(e.__proto__.__proto__ === A.prototype) //true

instanceof的原理

function getIsPrototype(source, target) {
      if (!["function", "object"].includes(typeof source) || source == null) {
        return false;
      };
      var prototype = source.__proto__;
      while(true) {
        if (prototype == null) {
          return false;
        };

        if (prototype === target.prototype) {
          return true;
        };

        prototype = prototype.__proto__;
      }
    };

3.Object.prototype.toString.call 精确判断数据类型

js的一切都是对象;Object.prototype上面有toString的方法;Object.prototype.toString.call(data)实际上是让data指向去Object.prototype;然后调用Object.prototype.toString()的方法

    console.log(Object.prototype.toString.call([])); "[object Array]"
    console.log(Object.prototype.toString.call(2)); "[object Number]"
    console.log(Object.prototype.toString.call('您好')); "[object String]"
    console.log(Object.prototype.toString.call(true)); "[object Boolean]"
    console.log(Object.prototype.toString.call({})); "[object Object]"
    console.log(Object.prototype.toString.call(null)); "[object Null]"
    console.log(Object.prototype.toString.call(undefined)); "[object Undefined]"
    console.log(Object.prototype.toString.call(function a() {})); "[object Function]"

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值