Get the class name of the JavaScript user-defined class

xiaoxiao2021-03-06  37

Get the class name of the JavaScript user-defined class

We know that although JavaScript is an object-based language. However, using its prototype characteristics, we can fully realize the OO complex framework of very SEXY. This can take a look at the articles of the Classical Forum 'basically achieve JavaScript OOP (0423 version)'. However, although we implemented this concept of 'class', JavaScript's script system is still unrecognizable. We don't have a way to get the type of custom class in the script system, such as '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 its: var jsclass = new jsclass (); but if you use Alert (Typeof (JSClass), we can only get 'Object'. Instead, use Alert (JSClass), we get '[class jsclass]', which is the result of the object instance default to the toString () method. Of course, we can return class name "jsclass" to return using the toString () method, but this method of manual TYPE to ensure that the correctness is ideal. So we demonstrate it from the class definition itself. Since the object (Object) in JavaScript implements the toString () method, and the function object's toString () method is the definition of the function, so we can Get the class name by handling the class definition. We obtain the definition of its constructor through the constructor property of the object instance, and the name of the constructor is also a class name of the JavaScript user-defined class. For the example above, execute var strfun = jaclass.constructor.toString (), Strfun is a string of the original statement definition of the constructor (the same as the contents of the above sentence). We take the "function name" from Strfun (class name), but you need to pay attention here. The Function class instance performs toString () and does not format the code, such as we write the JSClass constructor as the following format:

FUNCTION

Jsclass () {

THIS

.Attribute1

=

NULL

;

THIS

.Attribute2

=

NULL

;

//

...

}

This looks like this Strfun after executing toString (). Therefore, the acquisition class name also needs special care, the code __typeof__ code is as follows:

FUNCTION

__typeof __ (bjclass) {

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:

Alert (__ typeof __ (jsclass));

Alert (__ typeof __ (jsclass));

Alert (__ typeof __ (1));

Alert (__ typeof __ ([]));

Alert (__ typeof __ (}));

The results are: "JSClass", "Function", "Number", "array", and "Object".

Here you need to pay attention to two places. Number, array or objece, and the type name obtained using __typeof__ is matched with its type name, the first letter is capitalized.

转载请注明原文地址:https://www.9cbs.com/read-57575.html

New Post(0)