Home >Web Front-end >JS Tutorial >Code to get the class name of a JavaScript user-defined class_javascript tips
We know that although JavaScript is an object-based language. But using its prototype feature, we can completely implement a very sexy OO programming framework. For this, you can read the classic forum article 'Basically implement OOP of javascript (version 0423)'.
But although we have implemented the concept of 'class', the JavaScript scripting system still does not accept it. We have no way to use the typeof method in the script system to obtain the type of a 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( )
;
}
We generate an instance of it: var jsclass = new JSClass();
But if we use alert(typeof(jsclass)), we can only get 'object'. Instead, 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 this method of relying on manual typing to ensure correctness is not always ideal.
So we think of a solution from the class definition itself, because the objects in JavaScript all implement the toString() method by default, and the toString() method of the function object (Function) returns the definition of the function itself, so that we can get the class name by processing the class definition.
We can get the definition of its constructor through the constructor attribute of the object instance, and the name of the constructor is the class name of the JavaScript user-defined class. For the above example, execute var strFun = jaclass.constructor.toString(), strFun is the string of the original statement definition of the constructor (the same as the content of the above statement block). We just need to take out the "function name" (class name) from strFun, but we need to pay attention here. Instances of the Function class do not format the code when executing toString(). For example, we write the constructor of JSClass in the following format:
function
JSClass
(
)
{
this.Attribute1 = null;
this.Attribute2 = null;
// ...
}
The code in strFun after executing toString() also looks like this.
So you need to be careful when getting the class name. The code of 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:
The results are: "JSClass", "Function", "Number", "Array" and "Object". > Two things need to be noted here. One is: the difference between jsclass and JSClass. jsclass is a class instance, but the return type of JSClass is Function; the second is that if it is a system type, the types obtained by using typeof are all lowercase. For example, number, array or object, etc., and the type name obtained using __typeof__ matches its type name, with the first letter in capital letters
.