zoukankan      html  css  js  c++  java
  • js 判断各种数据类型 typeof 几种类型值

    了解js的都知道, 有个typeof  用来判断各种数据类型,有两种写法:typeof   xxx   ,typeof(xxx)

           如下实例:

           typeof   2      输出   number
           typeof   null   输出   object

           typeof   {}    输出   object

           typeof    []    输出   object

           typeof   (function(){})   输出  function

           typeof    undefined         输出  undefined

           typeof   '222'                 输出    string

          typeof  true                   输出     boolean

    (es6新增了Symbol)

        这里面包含了js里面的五种数据类型  number   string    boolean   undefined     object和函数类型 function

    看到这里你肯定会问了:我怎么去区分对象,数组和null呢?

         接下来我们就用到另外一个利器:Object.prototype.toString.call

         这是对象的一个原生原型扩展函数,用来更精确的区分数据类型。

         我们来试试这个玩儿意儿:

         var   gettype=Object.prototype.toString【注意,这里我测试的时候这么写不行,要直接Object.prototype.toString.call(...)来输出才行不知道为什么】

            gettype.call('aaaa')        输出      [object String]

            gettype.call(2222)         输出      [object Number]

            gettype.call(true)          输出      [object Boolean]

            gettype.call(undefined)  输出      [object Undefined]

            gettype.call(null)                  输出   [object Null]

             gettype.call({})                   输出   [object Object]

             gettype.call([])                    输出   [object Array]
             gettype.call(function(){})     输出   [object Function]

          看到这里,刚才的问题我们解决了。 它还可以判断document , body 等等,继续看下面;

    注意,这种方法没法测试自定义的类型(可能是自定义类型需要实现什么东东?)

     jquery下面也有一个判断类型的如下:

    • 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(/test/) === "regexp"

     constructor也能判断数据类型:【对自定义类型也是不行的】

         如:''.constructor==String    

               [].constructor==Array

               var obj= new Object()   obj.constructor==Object

          其实js 里面还有好多类型判断      [object HTMLDivElement]     div 对象  ,    [object HTMLBodyElement]  body 对象    ,[object Document](IE)或者  [object HTMLDocument](firefox,google) ......各种dom节点的判断,这些东西在我们写插件的时候都会用到。

         可以封装的方法如下  :

          var   gettype=Object.prototype.toString

         var    utility={

              isObj:function(o){

                   return    gettype.call(o)=="[object Object]";

              },

              isArray:function(o){

                   return    gettype.call(o)=="[object Array]";

              },

              isNULL:function(o){

                   return    gettype.call(o)=="[object Null]";

              },

              isDocument:function(){

                    return    gettype.call(o)=="[object Document]"|| [object HTMLDocument];

              }

              ........

        }

    总结,判断自定义类型还是用instanceof比较靠谱

    转自:https://www.cnblogs.com/chaoyuehedy/p/7885277.html

  • 相关阅读:
    智慧养老民政监管平台建设方案
    CF600E Lomsat gelral dsu on tree
    dsu on tree详解
    【Spring 从0开始】Spring5 新功能,整合日志框架 Log4j2
    【Spring 从0开始】JdbcTemplate 数据库事务管理
    【Spring 从0开始】JdbcTemplate 数据库事务参数
    【Spring 从0开始】JdbcTemplate 数据库事务管理
    【Spring 从0开始】JdbcTemplate 操作数据库
    【Spring 从0开始】AOP 操作
    【Spring 从0开始】AOP 操作中的相关术语、环境准备
  • 原文地址:https://www.cnblogs.com/silentdoer/p/11811760.html
Copyright © 2011-2022 走看看