Compared with the general OO language, JavaScript lacks a built-in function (such as the GetType of C #) to obtain the object class name ). The built-in typeof function in Javascript only returns one "object" when judging arrays and objects. Of course, this is also true for user-defined types. The following Code Clarified this point:
Code
1 functionmyclassa (){
2}
3
4 varobj = newobject ();
5 vararr = newarray ();
6 varmyobj = newmyclassa ();
7
8document. Write (typeof (OBJ) + "<br/> ");
9document. Write (typeof (ARR) + "<br/> ");
10document. Write (typeof (myobj) + "<br/> ");
11
The above code will output:
Object
Object
Object
However, a JavaScript Object also has a constructor attribute (except for some built-in objects, such as Windows and document) that points to the constructor of the object ), the object constructor name is the same as the object type name, And the constructor name can be parsed from the constructor attribute string. The following code uses this mechanism to obtain the class name of an existing object. The returned value is the defined class name or undefined.
Code
1/* returnstheclassnameoftheargumentorundefinedif
2 It 'snotavalid1_criptobject.
3 */
4 functiongetobjectclass (OBJ ){
5 If (OBJ & obj. constructor & obj. constructor. tostring ){
6 vararr = obj. constructor. tostring (). Match (
7/functions * (W + )/);
8
9 If (ARR & arr. Length = 2 ){
10 returnarr [1];
11}
12}
13
14 returnundefined;
15}
The following are examples of using the getobjectclass method and the built-in typeof method. They are used to test various types of objects:
Code
1 functionmyclass (){
2}
3
4 functiontest (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
17 test ("integer", "42 ");
18 test ("Boolean", "true ");
19 test ("string", "" helloworld! "");
20 test ("function", "myclass ");
21 test ("regularexpression", "/matchthis! /");
22 test ("intrinsicobject", "document ");
23 test ("arrayobject", "newarray (1, 2, 3 )");
24 test ("dateobject", "newdate ()");
25 test ("objectobject", "newobject ()");
26 test ("myclassobject", "newmyclass ()");
The output is as follows:
Code
Integer: 42
-Tostring () returns: 42
-Typeof () returns: Number
-Getobjectclass () returns: Number
Boolean: True
-Tostring () returns: True
-Typeof () returns: Boolean
-Getobjectclass () returns: Boolean
String: "helloworld! "
-Tostring () returns: helloworld!
-Typeof () returns: String
-Getobjectclass () returns: String
Function: myclass
-Tostring () returns: functionmyclass (){}
-Typeof () returns: Function
-Getobjectclass () returns: Function
Regularexpression:/matchthis! /
-Tostring () returns:/matchthis! /
-Typeof () returns: Object
-Getobjectclass () returns: Regexp
Intrinsicobject: Document
-Tostring () returns: [object]
-Typeof () returns: Object
-Getobjectclass () returns: Undefined
Arrayobject: newarray (1, 2, 3)
-Tostring () returns: 1, 2, 3
-Typeof () returns: Object
-Getobjectclass () returns: Array
Dateobject: newdate ()
-Tostring () returns: tueaug1917: 32: 34utc + 08002008
-Typeof () returns: Object
-Getobjectclass () returns: Date
Objectobject: newobject ()
-Tostring () returns: [objectobject]
-Typeof () returns: Object
-Getobjectclass () returns: Object
Myclassobject: newmyclass ()
-Tostring () returns: [objectobject]
-Typeof () returns: Object
-Getobjectclass () returns: myclass
We can see that in the test of arrays, objects, and custom type objects, the typeof method can only return "object", while the getobjectclass method will get the real type name.