從typeof()用法深入分析對象類型
<script
type="text/javascript">
document.write("typeof()的用法<br>");
document.write(typeof("")
+ "<br>"); //string
document.write(typeof("hi") +
"<br>");//string
document.write(typeof(200) + "<br>");
//number
document.write(typeof(0) + "<br>");
//number
document.write(typeof(false) + "<br>");
//boolean
document.write(typeof(true) + "<br>");
//boolean
document.write(typeof(new Object()) + "<br>");
//object
document.write(typeof(new Array())+"<br>");
//object
document.write(typeof(new Date())+"<br>");
//object
document.write(typeof(new RegExp())+"<br>");
//object
document.write(typeof(null) + "<br>");
//object
document.write(typeof(new Function())+"<br>");
//function
document.write(typeof(undefined) + "<br>");
//undefined
</script>
結(jié)果如下:
typeof()的用法
string
string
number
number
boolean
boolean
object
object
object
object
object
function
undefined
由代碼結(jié)果分析得:
typeof(null),typeof(Object),typeof(Array),typeof(Date),typeof(RegExp)結(jié)果都是object;
typeof()方法只能分辨出number、string、function、boolean數(shù)據(jù)類型。
那么如何能清楚分辨出其他Array、Date..類型呢?
這里有一個有用的技巧就是運用Object.toString()方法來測試對象類型,其默認的返回形式如下的字符串:[object
class],這里的class可以是用戶定義的任何對象,如Window,Object,Array等。我們只需要查看class的值就能辨別出對象的類型。
好,看實驗吧!
這里我創(chuàng)建了一個checkType函數(shù),用來檢測對象類型。
<script language="javascript">
var num =
1221;
var str = "abc123";
var obj =
{x:2,y:3};
var arr = [3,"df",{}];
var date = new Date();
var func =
function(){};
var error = new
Error();
var exp = new RegExp();
var
n = null;
var un;
document.writeln(checkType(num));
document.writeln(checkType(str));
document.writeln(checkType(obj));
document.writeln(checkType(arr));
document.writeln(checkType(date));
document.writeln(checkType(func));
document.writeln(checkType(error));
document.writeln(checkType(func));
document.writeln(checkType(exp));
document.writeln(checkType(n));
document.writeln(checkType(un));
function checkType(x){
var typeAll =
Object.prototype.toString.apply(x);
return
typeAll.substring(8,typeAll.length-1);
}
</script>
結(jié)果:
Number String Object Array Date Function
Error Function RegExp Window Window
type="text/javascript">
+ "<br>");
"<br>");//string
//number
//number
//boolean
//boolean
//object
//object
//object
//object
//object
//function
//undefined
</script>
結(jié)果如下:
typeof()的用法
string
string
number
number
boolean
boolean
object
object
object
object
object
function
undefined
由代碼結(jié)果分析得:
typeof(null),typeof(Object),typeof(Array),typeof(Date),typeof(RegExp)結(jié)果都是object;
typeof()方法只能分辨出number、string、function、boolean數(shù)據(jù)類型。
那么如何能清楚分辨出其他Array、Date..類型呢?
這里有一個有用的技巧就是運用Object.toString()方法來測試對象類型,其默認的返回形式如下的字符串:[object
class],這里的class可以是用戶定義的任何對象,如Window,Object,Array等。我們只需要查看class的值就能辨別出對象的類型。
好,看實驗吧!
這里我創(chuàng)建了一個checkType函數(shù),用來檢測對象類型。
<script language="javascript">
1221;
{x:2,y:3};
var date = new Date();
function(){};
Error();
n = null;
document.writeln(checkType(num));
document.writeln(checkType(str));
document.writeln(checkType(obj));
document.writeln(checkType(arr));
document.writeln(checkType(date));
document.writeln(checkType(func));
document.writeln(checkType(error));
document.writeln(checkType(func));
document.writeln(checkType(exp));
document.writeln(checkType(n));
document.writeln(checkType(un));
function checkType(x){
Object.prototype.toString.apply(x);
return
typeAll.substring(8,typeAll.length-1);
}
</script>
結(jié)果:
Number String Object Array Date Function
Error Function RegExp Window Window