Obtain the Javascript variable type

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.