js 判断各种数据类型

原文:js 判断各种数据类型

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

看到这里,刚才的问题我们解决了。

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

}

........

}

今天讲的内容不是很多,大家对于当前所讲的东西有啥要补充的可以留个言。。。

时间: 2024-08-16 23:49:30

js 判断各种数据类型的相关文章

js判断复合数据类型的两种方式(typeof不奏效了)

js判断复合数据类型的两种方式(typeof不奏效了) 博客分类: Web前端-JS语言核心 JavaScript 作者:zccst typeof认为所有的复合数据类型都是"object",没法进一步细分,所以还得用其他方法 先上结论: 1,(arr && typeof(arr) === "object" && arr.constructor === Array) 2,Object.prototype.toString.call(ar

(转)js 判断各种数据类型

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

JS判断任何数据类型

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <script> alert( deterType(12) ); function deterType(obj){ var tmp = {}.toString.call(obj).split(' ')[1

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

js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: 什么整数啊浮点数啊都叫数字,你懂的~ Boolean:  就是true和false啦 undefined: 未定义,就是你创建一个变量后却没给它赋值~ null:  故名思久,null就是没有,什么也不表示 object: 这个我也很难解释的说.就是除了上面五种之外的类型 如何判断js中的数据类型:type

如何判断js中的数据类型

如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstring."; var b = 222; var c= [1,2,3]; var d = new Date(); var e = function(){alert(111);}; var f = function(){this.name="22";}; 最常见的判断方法:typeof

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

原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstring."; var b = 222; var c= [1,2,3]; var d = new Date(); var e = function(){alert(111);}; var

判断js中的数据类型

如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstring."; var b = 222; var c= [1,2,3]; var d = new Date(); var e = function(){alert(111);}; var f = function(){this.name="22";}; 最常见的判断方法:typeof

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

判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异同. 先举几个例子: 1 var a = "iamstring."; 2 var b = 222; 3 var c= [1,2,3]; 4 var d = new Date(); 5 var e = function(){alert(111);}; 6 var f = function()

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

判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异同. 先举几个例子: var a = "iamstring."; var b = 222; var c= [1,2,3]; var d = new Date(); var e = function(){alert(111);}; var f = function(){this.name=&