咱们知道,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