导航:首页 > 解决方法 > js数据类型的检测方法

js数据类型的检测方法

发布时间:2022-07-25 19:37:52

❶ js如何判断类型

typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型。返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 7 种:number、boolean、symbol、string、object、undefined、function 等。

有些时候,typeof 操作符会返回一些令人迷惑但技术上却正确的值:

❷ JS类型判断的几种方式

检测简单的数据类型的方法

❸ 关于JavaScript的变量的数据类型的判断方法

虽然Javascript是弱类型语言,但是,它也有自己的几种数据类型,分别是:Number、String、Boolean、Object、Udefined、Null。其中,Object属于复杂数据类型,Object
由无序的键值对组成。其余几种都属于简单数据类型。注意:变量类型首字母大写,而变量值首字母是小写的。

JavaScript不支持自定义类型,所以JavaScript中的所有值都属于这六种类型之一。

要搞清楚一个变量是何种数据类型,就要使用typeof操作符了,注意,尤其要注意的是,typeof是操作符,不是方法,因此,typeof中的字母'o'是小写的。

语法:typeof
temp;
//temp是一个变量,这里可以不加括号,但是为了程序的可读性,最好还是加上括号。
JavaScript
本身可以用它typeof来检测变量的类型,但是有些结果却让人疑惑,例如,数组的类型居然是"Object"。
下面是用typeof对各种数据类型的判断结果
var
myFunction
=
function()
{
console.log('hello');
};
var
myObject
=
{
foo
:
'bar'
};
var
myArray
=
[
'a',
'b',
'c'
];
var
myString
=
'hello';
var
myNumber
=
3;
typeof
myFunction;
//
返回
'function'
typeof
myObject;
//
返回
'object'
typeof
myArray;
//
返回
'object'
--
小心哦!
typeof
myString;
//
返回
'string';
typeof
myNumber;
//
返回
'number'
typeof
null;
//
返回
'object'
--
小心哦!
if
(myArray.push
&&
myArray.slice
&&
myArray.join)
{
//
很可能是一个数组
//
当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子。
}
if
(Object.prototype.toString.call(myArray)
===
'[object
Array]')
{
//
肯定是一个数组!
//
这是判断一个变量是否为数组的最可靠方法
}

❹ Javascript如何判断数据类型和数组类型

通过下面方法进行检测,如果是数组,则返回true,如果不是数组,则返回false

function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; }

测试:

var a1 = [1,2,3];var a2=new Array("a","b");var a3={a:1};alert(isArray(a1));//truealert(isArray(a2));//truealert(isArray(a3));//false

❺ 如何正确判断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 undefinedobject和函数类型 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节点的判断,这些东西在我们写插件的时候都会用到。
可以封装的方法如下:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

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中对象的类型

如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较<br><br>如何判断js中的类型呢,先举几个例子:<br><br>var a = "iamstring.";<br><br>var b = 222;<br><br>var c= [1,2,3];<br><br>var d = new Date();<br><br>var e =<br>function(){alert(111);};<br><br>var f =<br>function(){this.name="22";};<br><br>最常见的判断方法:typeof<br><br>alert(typeof a)<br> ------------> string<br><br>alert(typeof b)<br> ------------> number<br><br>alert(typeof c)<br> ------------> object<br><br>alert(typeof d)<br> ------------> object<br><br>alert(typeof e)<br> ------------> function<br><br>alert(typeof f)<br> ------------> function<br><br>其中typeof返回的类型都是字符串形式,需注意,例如:<br><br>alert(typeof a == "string")<br>-------------> true<br><br>alert(typeof a == String)<br>---------------> false<br><br>另外typeof<br>可以判断function的类型;在判断除Object类型的对象时比较方便。<br><br>判断已知对象类型的方法: instanceof<br><br>alert(c instanceof Array)<br>---------------> true<br><br>alert(d instanceof<br>Date) <br><br>alert(f instanceof Function)<br>------------> true<br><br>alert(f instanceof function)<br>------------> false<br><br>注意:instanceof<br>后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。<br><br>根据对象的constructor判断:<br>constructor<br><br>alert(c.constructor ===<br>Array) ----------> true<br><br>alert(d.constructor === Date)<br>-----------> true<br><br>alert(e.constructor ===<br>Function) -------> true<br><br>注意: constructor 在类继承时会出错<br><br>eg,<br><br>function A(){};<br><br>function B(){};<br><br>A.prototype = new B(); //A继承自B<br><br>var aObj = new A();<br><br>alert(aobj.constructor === B) -----------><br>true;<br><br>alert(aobj.constructor === A) -----------><br>false;<br><br>而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:<br><br>alert(aobj instanceof B) ----------------><br>true;<br><br>alert(aobj instanceof B) ----------------><br>true;<br><br>言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:<br><br>aobj.constructor = A;<br>//将自己的类赋值给对象的constructor属性<br><br>alert(aobj.constructor === A) -----------><br>true;<br><br>alert(aobj.constructor === B) -----------><br>false; //基类不会报true了;<br><br>通用但很繁琐的方法: prototype<br><br>alert(Object.prototype.toString.call(a) === ‘[object String]’)<br>-------> true;<br><br>alert(Object.prototype.toString.call(b) === ‘[object Number]’)<br>-------> true;<br><br>alert(Object.prototype.toString.call(c) === ‘[object Array]’)<br>-------> true;<br><br>alert(Object.prototype.toString.call(d) === ‘[object Date]’)<br>-------> true;<br><br>alert(Object.prototype.toString.call(e) === ‘[object Function]’)<br>-------> true;<br><br>alert(Object.prototype.toString.call(f) === ‘[object Function]’)<br>-------> true;<br><br>大小写不能写错,比较麻烦,但胜在通用。<br><br>通常情况下用typeof<br>判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,简单总结下,挖个坑,欢迎补充!

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

typeof算是最常见的了,使用它会返回一个字符串,适合函数对象和基本类型(js中的基本类型:number、string、boolean、null、undefined、object[对象])的判断。

console.log("测试number:"+typeof 1); console.log("测试string:"+typeof "str");

console.log("测试false:"+typeof false); console.log("测试null:"+typeof null);

console.log("测试undefined:"+typeof undefined); console.log("测试Object:"+typeof new Object());

console.log("测试Object:"+typeof new Array());

console.log("看看typeof NaN是啥:"+typeof NaN);

console.log("我想看看数组[1,2,3]类型:"+typeof [1,2,3]);

console.log("看看function是啥:"+typeof function(){});

阅读全文

与js数据类型的检测方法相关的资料

热点内容
伤口消炎的最简单方法 浏览:26
鸡蛋清快速发酵方法 浏览:944
渗出液与漏出液的鉴别方法 浏览:736
消防师复习方法和技巧 浏览:114
叶面积测定方法的比较研究 浏览:343
胶原蛋白检测方法 浏览:452
纸的制作方法图片大全 浏览:238
薄荷叶怎么种植方法 浏览:448
难治性心绞痛治疗方法 浏览:796
治疗咽喉炎好方法 浏览:94
鞋子怎么去霉点最好方法 浏览:789
方法怎么说二颜色 浏览:763
如何食用灵芝的方法 浏览:662
幼儿注意不善于转移的解决方法 浏览:351
请问狐臭治疗方法 浏览:183
肩周炎腱鞘炎的区别及治疗方法 浏览:739
宿舍手洗衣服方法如何 浏览:779
男性腰垫锻炼方法 浏览:988
蒸汽蒸脸的正确方法 浏览:477
lg洗衣机的使用方法 浏览:135