您的位置:首页 > 移动开发 > Objective-C

How do I get the name of an object's type in JavaScript

2015-10-06 21:30 826 查看
这群逆天的家伙,天天脑子在想什么?
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://blog.csdn.net/cs_wuxiang/article/details/7724133 国内也有厉害角色 http://stackoverflow.com/questions/1249531/how-to-get-a-javascript-objects-class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: