面试官提问js篇1:js数据类型有哪些,以及检测数据类型的方法

前言:js面试题,先更新点简单的。(持续更新js面试题)

  1. js的数据类型(难度:★)

    答:基本数据类型:Number类型,String类型,Booolean类型,Null类型,Undefined类型,

    复杂数据类型:Array类型,Object类型,Function类型

  2. 如何判断变量的数据类型,有几种方法(难度:★)

    答:

    • typeof

      <script>
              var a = "iamstring.";
              var b = 222;
              var c = [1, 2, 3];
              var d = new Date();
              var e = function () { alert(111); };
              var f = function () { this.name = "22"; };
              console.log(typeof a);//string
              console.log(typeof b);//number
              console.log(typeof c);//object
              console.log(typeof d);//object
              console.log(typeof e);//function
              console.log(typeof f);//function
          </script>
      
    • instanceof判断构造函数的prototype是否出现再实例对象的原型链上。

              console.log(a instanceof Object);//false
              console.log(b instanceof Number);//false
              console.log(c instanceof Array);//true
              console.log(d instanceof Date);//true
              console.log(e instanceof Function);//true
              console.log(f instanceof Function);//true
      
    • constructor返回创建实例对象的 Object 构造函数的引用。(对象.constructor)

       		console.log(a.constructor === String)// true
              console.log(b.constructor === Number)// true
              console.log(c.constructor === Array)// true
              console.log(d.constructor === Date)// true
              console.log(e.constructor === Function)//true
      
    • Object.prototype.toString.call()===’[object Array]’

      		console.log(Object.prototype.toString.call(a) === '[object String]')// true;
              console.log(Object.prototype.toString.call(b) === '[object Number]') // true;
              console.log(Object.prototype.toString.call(c) === '[object Array]')// true;
              console.log(Object.prototype.toString.call(d) === '[object Date]') //true;
              console.log(Object.prototype.toString.call(e) === '[object Function]') //true;
              console.log(Object.prototype.toString.call(f) === '[object Function]')//true;
      
    • jQuery.type().无敌万能的方法。

      1. ​ 如果对象是undefined或null,则返回undefined或null。

      2. 如果对象有一个内部的对象和一个浏览器的内置对象,我们返回相应的对象名称。

         jQuery.type(undefined) === "undefined"
                jQuery.type() === "undefined"
                jQuery.type(window.notDefined) === "undefined"
                jQuery.type(null) === "null"
                jQuery.type(true) === "boolean"
                jQuery.type(3) === "number"
                jQuery.type("test") === "string"
                jQuery.type(function () { }) === "function"
                jQuery.type([]) === "array"
                jQuery.type(new Date()) === "date"
                jQuery.type(new Error()) === "error"
                jQuery.type(/test/) === "regexp"
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咖啡壶子

你的鼓励奖是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值