JavaScript isArray()函數(shù)判斷對象類型的種種方法
更新時(shí)間:2010年10月11日 19:25:02 作者:
我們知道,JavaScript中檢測對象類型的運(yùn)算符有:typeof、instanceof,還有對象的constructor屬性
1) typeof 運(yùn)算符
typeof 是一元運(yùn)算符,返回結(jié)果是一個(gè)說明運(yùn)算數(shù)類型的字符串。如:"number","string","boolean","object","function","undefined"(可用于判斷變量是否存在)。
但 typeof 的能力有限,其對于Date、RegExp類型返回的都是"object"。如:
typeof {}; // "object"
typeof []; // "object"
typeof new Date(); // "object"
所以它只在區(qū)別對象和原始類型的時(shí)候才有用。要區(qū)一種對象類型和另一種對象類型,必須使用其他的方法。如:instanceof 運(yùn)算符或?qū)ο蟮?constructor 屬。
2)instanceof 運(yùn)算符。
instanceof 運(yùn)算符要求其左邊的運(yùn)算數(shù)是一個(gè)對象,右邊的運(yùn)算數(shù)是對象類的名字或構(gòu)造函數(shù)。如果 object 是 class 或構(gòu)造函數(shù)的實(shí)例,則 instanceof 運(yùn)算符返回 true。如果 object 不是指定類或函數(shù)的實(shí)例,或者 object 為 null,則返回 false。如:
[] instanceof Array; // true
[] instanceof Object; // true
[] instanceof RegExp; // false
new Date instanceof Date; // true
所以,可以用instanceof運(yùn)算符來判斷對象是否為數(shù)組類型:
function isArray(arr)
{
return arr instanceof Array;
}
3)constructor 屬性。
JavaScript中,每個(gè)對象都有一個(gè)constructor屬性,它引用了初始化該對象的構(gòu)造函數(shù),常用于判斷未知對象的類型。如給定一個(gè)求知的值通過typeof運(yùn)算符來判斷它是原始的值還是對象。如果是對象,就可以使用constructor屬性來判斷其類型。所以判斷數(shù)組的函數(shù)也可以這樣寫:
function isArray(arr)
{
return typeof arr == "object" && arr.constructor == Array;
}
很多情況下,我們可以使用instanceof運(yùn)算符或?qū)ο蟮腸onstructor屬性來檢測對象是否為數(shù)組。例如很多JavaScript框架就是使用這兩種方法來判斷對象是否為數(shù)組類型。
但是檢測在跨框架(cross-frame)頁面中的數(shù)組時(shí),會失敗。原因就是在不同框架(iframe)中創(chuàng)建的數(shù)組不會相互共享其prototype屬性。例如:
<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>
<body>
<iframe></iframe>
</body>
在Ajaxian上看到了一種精確的檢測方法,跨原型鏈調(diào)用toString()方法:Object.prototype.toString()??梢越鉀Q上面的跨框架問題。
當(dāng)Object.prototype.toString(o)執(zhí)行后,會執(zhí)行以下步驟:
1)獲取對象o的class屬性。
2)連接字符串:"[object "+結(jié)果(1)+"]"
3)返回 結(jié)果(2)
例如:
Object.prototype.toString.call([]); // 返回 "[object Array]"
Object.prototype.toString.call(/reg/ig); // 返回 "[object RegExp]"
這樣,我們就可以寫一個(gè)健壯的判斷對象是否為數(shù)組的函數(shù):
function isArray(arr)
{
return Object.prototype.toString.call(arr) === "[object Array]";
}
此種方法得到國外多個(gè)javaScript大師的認(rèn)可,在即將發(fā)布的jQuery 1.3中將使用這種方法來檢測數(shù)組。
prototype.js的一個(gè)維護(hù)者寫了下面這個(gè)函數(shù),用于獲取對象的類型名
/**
* 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];
};
擴(kuò)展一下,用于檢測各種對象類型:
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
typeof 是一元運(yùn)算符,返回結(jié)果是一個(gè)說明運(yùn)算數(shù)類型的字符串。如:"number","string","boolean","object","function","undefined"(可用于判斷變量是否存在)。
但 typeof 的能力有限,其對于Date、RegExp類型返回的都是"object"。如:
復(fù)制代碼 代碼如下:
typeof {}; // "object"
typeof []; // "object"
typeof new Date(); // "object"
所以它只在區(qū)別對象和原始類型的時(shí)候才有用。要區(qū)一種對象類型和另一種對象類型,必須使用其他的方法。如:instanceof 運(yùn)算符或?qū)ο蟮?constructor 屬。
2)instanceof 運(yùn)算符。
instanceof 運(yùn)算符要求其左邊的運(yùn)算數(shù)是一個(gè)對象,右邊的運(yùn)算數(shù)是對象類的名字或構(gòu)造函數(shù)。如果 object 是 class 或構(gòu)造函數(shù)的實(shí)例,則 instanceof 運(yùn)算符返回 true。如果 object 不是指定類或函數(shù)的實(shí)例,或者 object 為 null,則返回 false。如:
[] instanceof Array; // true
[] instanceof Object; // true
[] instanceof RegExp; // false
new Date instanceof Date; // true
所以,可以用instanceof運(yùn)算符來判斷對象是否為數(shù)組類型:
function isArray(arr)
{
return arr instanceof Array;
}
3)constructor 屬性。
JavaScript中,每個(gè)對象都有一個(gè)constructor屬性,它引用了初始化該對象的構(gòu)造函數(shù),常用于判斷未知對象的類型。如給定一個(gè)求知的值通過typeof運(yùn)算符來判斷它是原始的值還是對象。如果是對象,就可以使用constructor屬性來判斷其類型。所以判斷數(shù)組的函數(shù)也可以這樣寫:
function isArray(arr)
{
return typeof arr == "object" && arr.constructor == Array;
}
很多情況下,我們可以使用instanceof運(yùn)算符或?qū)ο蟮腸onstructor屬性來檢測對象是否為數(shù)組。例如很多JavaScript框架就是使用這兩種方法來判斷對象是否為數(shù)組類型。
但是檢測在跨框架(cross-frame)頁面中的數(shù)組時(shí),會失敗。原因就是在不同框架(iframe)中創(chuàng)建的數(shù)組不會相互共享其prototype屬性。例如:
復(fù)制代碼 代碼如下:
<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>
<body>
<iframe></iframe>
</body>
在Ajaxian上看到了一種精確的檢測方法,跨原型鏈調(diào)用toString()方法:Object.prototype.toString()??梢越鉀Q上面的跨框架問題。
當(dāng)Object.prototype.toString(o)執(zhí)行后,會執(zhí)行以下步驟:
1)獲取對象o的class屬性。
2)連接字符串:"[object "+結(jié)果(1)+"]"
3)返回 結(jié)果(2)
例如:
Object.prototype.toString.call([]); // 返回 "[object Array]"
Object.prototype.toString.call(/reg/ig); // 返回 "[object RegExp]"
這樣,我們就可以寫一個(gè)健壯的判斷對象是否為數(shù)組的函數(shù):
復(fù)制代碼 代碼如下:
function isArray(arr)
{
return Object.prototype.toString.call(arr) === "[object Array]";
}
此種方法得到國外多個(gè)javaScript大師的認(rèn)可,在即將發(fā)布的jQuery 1.3中將使用這種方法來檢測數(shù)組。
prototype.js的一個(gè)維護(hù)者寫了下面這個(gè)函數(shù),用于獲取對象的類型名
復(fù)制代碼 代碼如下:
/**
* 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];
};
擴(kuò)展一下,用于檢測各種對象類型:
復(fù)制代碼 代碼如下:
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
相關(guān)文章
document.createElement("A")比較不錯(cuò)的屬性
document.createElement("A")比較不錯(cuò)的屬性...2007-08-08
javascript 處理HTML元素必須避免使用的一種方法
我們在編寫前臺頁面的時(shí)候,可能經(jīng)常會用到“javascript+數(shù)據(jù)”生成頁面元素的方法,但當(dāng)我們要處理的數(shù)據(jù)量較大,導(dǎo)致頁面需要展現(xiàn)過多的控件的時(shí)候,頁面的響應(yīng)速度也會直線下降2009-07-07
深入理解 webpack 文件打包機(jī)制(小結(jié))
這篇文章主要介紹了深入理解 webpack 文件打包機(jī)制(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
淺析Javascript中bind()方法的使用與實(shí)現(xiàn)
下面小編就為大家?guī)硪黄獪\析Javascript中bind()方法的使用與實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,一起跟隨小編過來看看吧2016-04-04
JavaScript中立即執(zhí)行函數(shù)實(shí)例詳解
javascript和其他編程語言相比比較隨意,所以javascript代碼中充滿各種奇葩的寫法,有時(shí)霧里看花,當(dāng)然,能理解各型各色的寫法也是對javascript語言特性更進(jìn)一步的深入理解。這篇文章主要給大家介紹了關(guān)于JavaScript中立即執(zhí)行函數(shù)的相關(guān)資料,需要的朋友可以參考下。2017-11-11

