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:
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);
}
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.
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.