[JS面试] ES6新特性 & js判断数据类型的方式

你使用过ES6中的哪些新特性?

ES6 中提出了许多新特性,比如 let / const关键字、模板字符串、解构赋值、扩展运算符、箭头函数、for…of循环、Set、Promise等;
像我比较常用的有模板字符串、let / const 关键字声明变量和常量、箭头函数、Promise对象和新增的一些方法,比如字符串中的startsWith、padStart、padEnd,数组中的find方法查找元素、enties()方法和for…of循环结合遍历数组,Number中的 isNaN方法、对象中的Object.assign()方法用来深拷贝对象;

js判断数据类型的方式有哪些?

  1. typeof:typeof 算是最常见的了,使用它会返回一个字符串,适合函数、对象和基本类型的判断。

      console.log("测试number:"+typeof 1);
      console.log("测试string:"+typeof "str");
      console.log("测试false:"+typeof false);
      console.log("测试null:"+typeof null);
      console.log("测试undefined:"+typeof undefined);
      console.log("测试Object:"+typeof new Object());
      console.log("测试NaN"+typeof NaN);// Number NaN是Number类型的一个特殊值
      console.log("测试数组:"+typeof [1,2,3]);// Object
      console.log("测试函数类型:"+typeof function(){});// function
    
  2. instanceof:返回布尔类型,使用时要注意大小写

    [1,2,3] instanceof Array                -------->true
    new Date() instanceof Date              -------->true
    new Function() instanceof Function      -------->true
    new Function() instanceof function      -------->false
    null instanceof Object                  -------->false
    
  3. Object.prototype.toString.call() :适用于所有类型的判断检测,在Object原型上返回数据格式

     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([1, 2, 3]))       -------->[object Array]
     console.log(Object.prototype.toString.call(null))            -------->[object Null]
     console.log(Object.prototype.toString.call(undefined))       -------->[object Undefined]
     console.log(Object.prototype.toString.call({name: 'Hello'})) -------->[object Object]
     console.log(Object.prototype.toString.call(function () {}))  -------->[object Function]
     console.log(Object.prototype.toString.call(new Date()))      -------->[object Date]
     console.log(Object.prototype.toString.call(/\d/))            -------->[object RegExp]
     console.log(Object.prototype.toString.call(Symbol()))        -------->[object Symbol]
    
  4. jquery.type() :适用于jQuery项目,据说也是万能的方法

部分摘自 js判断数据类型常用的6种方法

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值