咱們知道,JavaScript中檢測對象類型的運算符有:typeof、instanceof,還有對象的constructor屬性: 1) typeof 運算符 typeof 是一元運算符,返回結果是一個說明運算數類型的字符串。如:"number","string","boolean","object","function","undefined"(可用於判斷變量是否存在)。 但 typeof 的能力有限,其對於Date、RegExp類型返回的都是"object"。如:html
1
2
3
|
typeof
{};
// "object"
typeof
[];
// "object"
typeof
new
Date();
// "object"
|
因此它只在區別對象和原始類型的時候纔有用。要區一種對象類型和另外一種對象類型,必須使用其餘的方法。如:instanceof 運算符或對象的 constructor 屬。 2)instanceof 運算符。 instanceof 運算符要求其左邊的運算數是一個對象,右邊的運算數是對象類的名字或構造函數。若是 object 是 class 或構造函數的實例,則 instanceof 運算符返回 true。若是 object 不是指定類或函數的實例,或者 object 爲 null,則返回 false。如:java
1
2
3
4
|
[]
instanceof
Array;
// true
[]
instanceof
Object;
// true
[]
instanceof
RegExp;
// false
new
Date
instanceof
Date;
// true
|
因此,能夠用instanceof運算符來判斷對象是否爲數組類型:數組
1
2
3
|
function
isArray(arr){
return
arr
instanceof
Array;
}
|
3)constructor 屬性。 JavaScript中,每一個對象都有一個constructor屬性,它引用了初始化該對象的構造函數,經常使用於判斷未知對象的類型。如給定一個求知的值 經過typeof運算符來判斷它是原始的值仍是對象。若是是對象,就能夠使用constructor屬性來判斷其類型。因此判斷數組的函數也能夠這樣寫:框架
1
2
3
|
function
isArray(arr){
return
typeof
arr ==
"object"
&& arr.constructor == Array;
}
|
不少狀況下,咱們能夠使用instanceof運算符或對象的constructor屬性來檢測對象是否爲數組。例如不少JavaScript框架就是使用這兩種方法來判斷對象是否爲數組類型。 可是檢測在跨框架(cross-frame)頁面中的數組時,會失敗。緣由就是在不一樣框架(iframe)中建立的數組不會相互共享其prototype屬性。例如:函數
1
2
3
4
5
6
7
|
<
script
>
window.onload=function(){
var iframe_arr=new window.frames[0].Array;
alert(iframe_arr instanceof Array); // false
alert(iframe_arr.constructor == Array); // false
}
</
script
>
|
在Ajaxian上看到了一種精確的檢測方法,跨原型鏈調用toString()方法:Object.prototype.toString()。能夠解決上面的跨框架問題。 當Object.prototype.toString(o)執行後,會執行如下步驟: 1)獲取對象o的class屬性。 2)鏈接字符串:"[object "+結果(1)+"]" 3)返回 結果(2) 例如:post
1
2
|
Object.prototype.toString.call([]);
// 返回 "[object Array]"
Object.prototype.toString.call(/reg/ig);
// 返回 "[object RegExp]"
|
這樣,咱們就能夠寫一個健壯的判斷對象是否爲數組的函數:this
1
2
3
|
function
isArray(arr){
return
Object.prototype.toString.call(arr) ===
"[object Array]"
;
}
|
此種方法獲得國外多個javaScript大師的承認,在即將發佈的jQuery 1.3中將使用這種方法來檢測數組。 prototype.js的一個維護者寫了下面這個函數,用於獲取對象的類型名spa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/**
* Returns internal [[Class]] property of an object
*
* Ecma-262, 15.2.4.2
* Object.prototype.toString( )
*
* When the toString method is called, the following steps are taken:
* 1. Get the [[Class]] property of this object.
* 2. Compute a string value by concatenating the three strings "[object ", Result (1), and "]".
* 3. Return Result (2).
*
* __getClass(5); // => "Number"
* __getClass({}); // => "Object"
* __getClass(/foo/); // => "RegExp"
* __getClass(''); // => "String"
* __getClass(true); // => "Boolean"
* __getClass([]); // => "Array"
* __getClass(undefined); // => "Window"
* __getClass(Element); // => "Constructor"
*
*/
function
__getClass(object){
return
Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
};
|
擴展一下,用於檢測各類對象類型:prototype
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var
is ={
types : [
"Array"
,
"Boolean"
,
"Date"
,
"Number"
,
"Object"
,
"RegExp"
,
"String"
,
"Window"
,
"HTMLDocument"
]
};
for
(
var
i = 0, c; c = is.types[i ++ ]; ){
is[c] = (
function
(type){
return
function
(obj){
return
Object.prototype.toString.call(obj) ==
"[object "
+ type +
"]"
;
}
)(c);
}
alert(is.Array([]));
// true
alert(is.Date(
new
Date));
// true
alert(is.RegExp(/reg/ig));
// true
|
轉載自:http://hi.baidu.com/mimimo/blog/item/2240974580104b3887947387.htmlcode