firefox and chrome will return the name from a constructor's name property-
var c1=new Car;
alert(c1.constructor.name)
Most browsers return undefined, however.
You could define a name
property in each constructor,
or dedicate a
method to return any object's constructor name.
Code:
function classof(what){
var tem, C= what.constructor;
tem= C? C.toString().match(/function\s+([a-zA-Z][\w\$]+)\s*\(/): '';
return tem? tem[1]: '';
}
Code:
//test case
function Car(specs){
//
}
var mycar= new Car();
alert('classof(mycar)='+ classof(mycar));
// it works as well for native objects
alert(classof(/\w/g));