Home Web Front-end JS Tutorial Code to get the class name of a JavaScript user-defined class_javascript tips

Code to get the class name of a JavaScript user-defined class_javascript tips

May 16, 2016 pm 07:17 PM

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
.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

jQuery Check if Date is Valid

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

jQuery get element padding/margin

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

10 jQuery Accordions Tabs

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

10 Worth Checking Out jQuery Plugins

HTTP Debugging with Node and http-console HTTP Debugging with Node and http-console Mar 01, 2025 am 01:37 AM

HTTP Debugging with Node and http-console

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

jquery add scrollbar to div

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

See all articles