Code used to obtain the class name of a 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 articles in the classic Forum '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 class name of the JavaScript user-defined class. 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. When the Function class instance executes toString (), the code is not formatted. For 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.

Related Article

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.