了解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

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

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

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

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

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

var   gettype=Object.prototype.toString

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];

}

........

}

js 判断各种数据类型 typeof 几种类型值的更多相关文章

  1. js 判断各种数据类型

    了解js的都知道, 有个typeof  用来判断各种数据类型,有两种写法:typeof   xxx   ,typeof(xxx) 如下实例: typeof   2      输出   number   ...

  2. Js的typeof和Js的基本数据类型

    本文将从以下几个方面介绍Js的typeof和Js的基本数据类型: ** Js的typeof的用法 ** Js的基本数据类型 ** 使用基本类型使用typeof的返回结果 ** Js的typeof的用法 ...

  3. 如何判断js中的数据类型?

    js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...

  4. 如何判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  5. [转]如何判断js中的数据类型

    原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructo ...

  6. 如何判断js中的数据类型(转)

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  7. 判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  8. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  9. 转:判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

随机推荐

  1. HttpWebRequest post上传文件

    public static string HttpUploadFile(string url, string path) { // 设置参数 HttpWebRequest request = WebR ...

  2. C# 筛选string 类型里面的汉字,获取首字母字母,正则表达式Regex 常用验证

    界面效果 1.提取汉字 private void buttonX1_Click(object sender, EventArgs e) { if (TxtYuan.Text.Trim() != &qu ...

  3. Jenkins 自动化部署asp.net

    使用步骤 1.安装jenkins.git和vs,并确保机器上安装了.net framework 4.5和.net framework4.0 ,完成后访问http://localhost:8080. 2 ...

  4. raiden_graph

    使用mermaid描述 raiden 通道 AB,正常状态 graph LR A-- 60,100,S_100 ---B 通道 AB closed graph LR A((A)) -. 60,100 ...

  5. 小规模kvm宿主机管理-webvirtmgr安装

    1.前言WebVirtMgr是近两年来发展较快,比较活跃,非常清新的一个KVM管理平台,提供对宿主机和虚机的统一管理,它有别于kvm自带的图形管理工具(virtual machine manager) ...

  6. Kotlin 区间和循环 Loop和Range

    在kotln中有一种很简单的方式来表达某一个区间的数字. 比如我想存储1~100. 我在其他的语言中可以用数组,步骤大概有三个把. 但是在kotlin中就只需要一步. 代码如下: fun main(a ...

  7. NSKeyedArchiver数据归档

    前言 在 OC 语言中,归档是一个过程,即用某种格式来保存一个或多个对象,以便以后还原这些对象. 通常,这个过程包括将(多个)对象写入文件中,以便以后读取该对象.可以使用归档的方法进行对象的深复制. ...

  8. 【bzoj5093】 [Lydsy1711月赛]图的价值 组合数+斯特林数+NTT

    Description "简单无向图"是指无重边.无自环的无向图(不一定连通). 一个带标号的图的价值定义为每个点度数的k次方的和. 给定n和k,请计算所有n个点的带标号的简单无向 ...

  9. winform跨线程问题(有参数和无参数)

    1.invoke是同步线程 using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...

  10. 【问题记录】Python运行报错:can only concatenate str (not "int") to str

    自己总是写程序时候用 + 拼接的时候忘记变量类型要一致,如下面 frame_num = "1" for i in range(1, frame_num + 1, 1): self. ...