Timo Rütten Timo Rütten - 1 year ago 99
Javascript Question

Get name of object or class in javascript

is there any solution to get the function name of an object ?

function alertClassOrObject (o) {
window.alert(o.objectName); //"myObj" OR "myClass" as a String
}

function myClass () {
this.foo = function () {
alertClassOrObject(this);
}
}

var myObj = new myClass();
myObj.foo();


for (var k in this) {...} - there is no information about the className or ObjectName. Is it possible to get one of them ? In PHP its possible - but i did not find any solution in the internet to get it in javascript.

Answer Source

Get your object's constructor function and then inspect its name property.

myObj.constructor.name

Returns "myClass".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download