关于检测数据类型,三种方法(typeof,instanceof,Object.prototype.toString.call())优缺点

  1.关于typeof 如下

 

  优点:能快速检查undefined,string,number,boolean类型

  缺点:当类型为object,null,array时都会返回object,所以不能区分这三类

  2.关于instanceof

 优点:能检测array,function,object类型 

 缺点:检测不了number,boolean,string

  3.object.prototype.toString.call()

 

   优点:能准确的判断所有的类型。

   缺点:写法过于繁琐

          最后.欢迎小伙伴进群交流讨论

原文地址:https://www.cnblogs.com/feifei0928/p/12185083.html