这群逆天的家伙,天天脑子在想什么?
http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript 仅仅推荐这个,剩下两个有心情可以读读
Is there a JavaScript equivalent of Java's class.getName()
?
No.
But here are various hacks that all fall down in one way or another:
Here is a hack that will do what you need - be aware that it modifies the Object's prototype, something people frown upon (usually for good reason)
Object.prototype.getName = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};
Now, all of your objects will have the function, getName()
, that will return the name of the constructor as a string. I have tested this in FF3 and IE7, I can't speak for other implementations.
http://stackoverflow.com/questions/1249531/how-to-get-a-javascript-objects-class