❶ js如何判斷類型
typeof 是一個操作符,其右側跟一個一元表達式,並返回這個表達式的數據類型。返回的結果用該類型的字元串(全小寫字母)形式表示,包括以下 7 種:number、boolean、symbol、string、object、undefined、function 等。
有些時候,typeof 操作符會返回一些令人迷惑但技術上卻正確的值:
對於基本類型,除 null 以外,均可以返回正確的結果。
對於引用類型,除 function 以外,一律返回 object 類型。
對於 null ,返回 object 類型。
對於 function 返回 function 類型。
其中,null 有屬於自己的數據類型 Null , 引用類型中的 數組、日期、正則 也都有屬於自己的具體類型,而 typeof 對於這些類型的處理,只返回了處於其原型鏈最頂端的 Object 類型,沒有錯,但不是我們想要的結果。
❷ JS類型判斷的幾種方式
檢測簡單的數據類型的方法
typeof方法用於檢測簡單的數據類型如typeof 12
instanceof的實例方法檢測如[] instanceof Array // true
arr.constructor == Array判斷arr的構造函數是否為數組,如果是則arr是數組
Array.isArray([])判斷是否是數組
精確判斷數據類型Object.prototype.toString.call(arr)
❸ 關於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(){});