Obtain the Class Name of the Javascript custom class

Source: Internet
Author: User

We know that JavaScript is an object-based language. However, with its prototype feature, we can fully implement a very sexy oo compiling framework. Here we can look at the Classic Forum'sArticle'Basically implement JavaScript OOP (version 0423 )'.

However, although we have implemented the 'class' concept, the Javascript script system still does not recognize it. We cannot use the typeof method in the script system to obtain the type of the custom class. For example, the 'class' jsclass is defined as follows:

Function Jsclass ()
{
This . Attribute1 =   Null ;
This . Attribute2 =   Null ;

This . Method1 =   Function ()
{
//...
};

This . Method2 =   Function ()
{
// ...
};

This . Tostring =   Function ()
{
Return '[Class jsclass]';
};
}

We generate an instance of the jsclass: var jsclass = new jsclass ();
However, if alert (typeof (jsclass) is used, we can only get 'object '. Instead, alert (jsclass) is used, but we get '[class jsclass]', which is the result of the default tostring () method called by the object instance. Of course, we can use the tostring () method to return the class name "jsclass", but this dependency is manual type to ensure correctness and is ideal from time to time.

So we can find a solution from the class definition itself, because the object in Javascript implements the tostring () method by default, and the tostring () of the function object (function () the method is to return the definition of the function, so that we can get the class name by processing the class definition.

Through the constructor attribute of the object instance, we can get the definition of its constructor, And the constructor name is the custom class of JavaScript.Class Name. In the preceding example, run var strfun = jaclass. constructor. tostring (). strfun is the string defined by the original statement of the constructor (the same as the block content of the preceding statement. You can retrieve the "function name" (Class Name) from strfun, but pay attention to it here. Function instances do not format when tostring () is executed.CodeFor example, we write the jsclass constructor in the following format:

Function
Jsclass
(
)
{
This . Attribute1 =   Null ;
This . Attribute2 =   Null ;
// ...
}

This is also the code in strfun after tostring () is executed.

Therefore, you need to be careful when getting the class name. The code for method _ typeof _ is as follows:

Function _ Typeof _ (objclass)
{
If (Objclass && Objclass. constructor)
{
VaR Strfun = Objclass. constructor. tostring ();
VaR Classname = Strfun. substr ( 0 , Strfun. indexof ('('));
Classname = Classname. Replace (' Function ','');
Return Classname. Replace ( / ( ^ \ S * ) | (\ S * $) / Ig ,'');
}
Return   Typeof (Objclass );
}

Example:

< Script Language = "JavaScript" >
Alert (_ typeof _ (jsclass ));
Alert (_ typeof _ (jsclass ));
Alert (_ typeof __(1));
Alert (_ typeof _ ([]);
Alert (_ typeof __({}));
</ Script >

The results are as follows: "jsclass", "function", "Number", "array", and "object ".

Note the following two points: the difference between jsclass and jsclass. jsclass is a class instance, while jsclass returns a function. The second is for the system type, the types obtained by using typeof are all in lowercase, such as number, array, or objece. The type names obtained by using _ typeof _ match the type names, uppercase letters.

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.