WEB开发网
开发学院网页设计JavaScript 获取JavaScript变量的类型(Finding Out Class Na... 阅读

获取JavaScript变量的类型(Finding Out Class Names of JavaScript Objects)

 2010-09-14 13:26:10 来源:WEB开发网   
核心提示: 下面是一些使用getObjectClass 方法和内置的typeof方法的例子,用它们分别测试了各种类型的对象:Code1functionMyClass(){2}34functiontest(type,expr){5varobj=eval(expr);67document.write(&q

下面是一些使用getObjectClass 方法和内置的typeof方法的例子,用它们分别测试了各种类型的对象:

Code

1functionMyClass(){ 
2} 
3 
4functiontest(type,expr){ 
5  varobj=eval(expr); 
6 
7  document.write("<p>"+type+":<b>"+ 
8    expr+"</b><br/>"); 
9  document.write("-toString()returns:"+ 
10    obj.toString()+"<br/>"); 
11  document.write("-typeof()returns:<b>"+ 
12    typeof(obj)+"</b><br/>"); 
13  document.write("-getObjectClass()returns:<b>"+ 
14    getObjectClass(obj)+"</b></p>"); 
15} 
16 
17test("Integer","42"); 
18test("Boolean","true"); 
19test("String",""HelloWorld!""); 
20test("Function","MyClass"); 
21test("Regularexpression","/Matchthis!/"); 
22test("Intrinsicobject","document"); 
23test("Arrayobject","newArray(1,2,3)"); 
24test("Dateobject","newDate()"); 
25test("Objectobject","newObject()"); 
26test("MyClassobject","newMyClass()"); 

Tags:获取 JavaScript 变量

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接