Get object name?

H

howa

A simple code like:

a = new User();

How can I print out the class name which a belong to?

Thanks.
 
K

Ketan Khairnar

A simple code like:

a = new User();

How can I print out the class name which a belong to?

Thanks.

not clear from this..
are you looking for something other than JavaScript then you are at
the wrong place...

if no then you need to define user

function User(){
this.name ="Ketan";
this.company="u4RIA";
this.className= function(){
return "USER";
}
}

this is sort of pseudo code and then you need to use
a = new User;
// get the type of the objuect using custome method
alert(a.className)

in all other scenarios it would give you type as "function"
 
G

GArlington

A simple code like:

a = new User();

How can I print out the class name which a belong to?

Thanks.

BTW: this question should go in comp.lang.java.* groups, NOT in
comp.lang.javascript
 
L

Lasse Reichstein Nielsen

howa said:
A simple code like:

a = new User();

How can I print out the class name which a belong to?

If you mean the name of the constructor function (Javascript has no
classes) then by default you can find the constructor method as:
var cons = a.constructor;
and you can extract the name from its string representation:

function functionName(func) {
var match = /function\s+(\w+)\b/.exec(func.toString());
if (match) {
return match[1];
} else {
return "[anonymous]";
}
}

Now, the big question is: What do you need this for?

Good luck
/L
 
N

Nikola Stjelja

Lasse said:
howa said:
A simple code like:

a = new User();

How can I print out the class name which a belong to?

If you mean the name of the constructor function (Javascript has no
classes) then by default you can find the constructor method as:
var cons = a.constructor;
and you can extract the name from its string representation:

function functionName(func) {
var match = /function\s+(\w+)\b/.exec(func.toString());
if (match) {
return match[1];
} else {
return "[anonymous]";
}
}

Now, the big question is: What do you need this for?

Good luck
/L
Straigth from the mozilla javascript manual:

function a()
{
}

var b = new a();
alert(b.constructor.name); //Alerts "a"
 
H

Henry

Straigth from the mozilla javascript manual:

function a()
{

}

var b = new a();
alert(b.constructor.name); //Alerts "a"

And a non-standard ECMAScript extension and so unlikely to be
available in many other web browsers. Certainly not supported by
JScript so not available in IE.
 
H

howa

Straigth from the mozilla javascript manual:

function a()
{

}

var b = new a();
alert(b.constructor.name); //Alerts "a"


Great tips, thx first!!


How about this:

var User = {};

User.sayHello = function() {
alert('hello');
}

var t = User.sayHello;

t();

Now I have variable t, how can I check it is pointing to
User.sayHello?


Howard
 
T

Thomas 'PointedEars' Lahn

Nikola said:
[...]
Straigth from the mozilla javascript manual:

function a()
{
}

var b = new a();
alert(b.constructor.name); //Alerts "a"

That allows for a nice optimization of my parser if JavaScript[tm] is used.
Thanks.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top