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

  1.关于typeof 如下

 

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

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

扫描二维码关注公众号,回复: 8554151 查看本文章

  2.关于instanceof

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

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

  3.object.prototype.toString.call()

 

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

   缺点:写法过于繁琐

猜你喜欢

转载自www.cnblogs.com/feifei0928/p/12185083.html