Get code _javascript tips for getting the class name of a JavaScript user-defined class

Source: Internet
Author: User
Tags class definition
We know that although JavaScript is an object-based (object-based) language. But using its prototype (prototype) feature, we can fully implement the very sexy OO framework, which can be seen in the Classic forum article ' essentially JavaScript OOP (version 0423) '.

However, although we have implemented the concept of ' class ', the scripting system of JavaScript is still not admit. We have no way to use the TypeOf method in the scripting system to get the type of the custom class, such as the ' class ' Jsclass definition 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 it: var jsclass = new Jsclass ();
But if you use alert (typeof (Jsclass)), we can only get ' object '. Instead of using alert (jsclass), we get ' [Class Jsclass] ', which is the result of the object instance calling the ToString () method by default. Of course we can use the ToString () method to return the class name "Jsclass", but the method that relies on manual type to ensure correctness is not ideal.

So we're going to do this from the class definition itself, because objects in JavaScript (object) implement the ToString () method by default, and the ToString () Method of function objects (functions) is to return the definition itself of the function. This allows us to get the class name by handling the class definition.

We can get the definition of its constructor through the constructor property of the object instance, and the name of the constructor is the class name of the JavaScript user custom class. For the above example, the Execute var strfun = jaclass.constructor.toString (), Strfun is the string of the original statement definition of the constructor (the same as the contents of the statement block above). Let's take the "Function name" (class name) out of the Strfun, but here's the note. The instance of a function class does not format the code when it executes ToString (), for example, we write the Jsclass constructor in the following format:

function
Jsclass
(
)
{
This. Attribute1 = null;
This. Attribute2 = null;
// ...
}
The code in the Strfun after the execution of ToString () is the same.

So getting the class name also requires special care, and the method __typeof__ code 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 were: "Jsclass", "Function", "number", "Array" and "Object".

Notice here two places, one is: the difference between Jsclass and Jsclass, Jsclass is a class instance, and Jsclass return type is a function oh; the second is that if the system type, the type that is used for typeof is lowercase, such as number, Array or objece, and the type name obtained by using __typeof__ matches its type name, and the first letter is capitalized.

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.