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
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)
Now, all of your objects will have the function,
http://blog.csdn.net/cs_wuxiang/article/details/7724133 国内也有厉害角色 http://stackoverflow.com/questions/1249531/how-to-get-a-javascript-objects-class
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
相关文章推荐
- 【python问题系列--3】TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
- [Training Video - 4] [Groovy] Object equality and variable equality check
- Object Oriented Basics(homework for 03)
- 黑马程序员—Objective-C内存管理
- OC中的单例模式
- java.lang.Object类常用方法。
- OC中得构造方法
- OC中的协议
- 黑马程序员----Objective-C学习笔记之autorelease基本使用
- 黑马程序员----Objective-C学习笔记之NSString类的内存管理问题
- 黑马程序员----Objective-C学习笔记之@class与循环retain问题
- 黑马程序员----Objective-C学习笔记之@property参数
- 黑马程序员----Objective-C学习笔记之单个对象内存管理
- 黑马程序员----Objective-C学习笔记之内存管理的原则
- 黑马程序员----Objective-C学习笔记之内存管理
- 【IOS 开发学习总结-OC-33】★★objective-c之文件 I/O概述+NSData/NSMutableData
- [Training Video - 3] [Groovy in Detail] Non-static and Static variables, objects and object referances
- objective-c复制
- OC中类的封装
- 【IOS 开发】Objective - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射