在javascript 中没有能够返回特定类型名的函数 如一个对象 console.log(obj);
得到的是[object HtmlTableCellElement] 如果想要一个函数能够返回HtmlTableCellElement js中默认没有这样的函数 可以自己实现一个
下面是我实现的一个可作参考:
- var getObjectClass = function (obj) {
- if (obj && obj.constructor && obj.constructor.toString()) {
-
- /*
- * for browsers which have name property in the constructor
- * of the object,such as chrome
- */
- if(obj.constructor.name) {
- return obj.constructor.name;
- }
- var str = obj.constructor.toString();
- /*
- * executed if the return of object.constructor.toString() is
- * "[object objectClass]"
- */
-
- if(str.charAt(0) == '[')
- {
- var arr = str.match(/\[\w+\s*(\w+)\]/);
- } else {
- /*
- * executed if the return of object.constructor.toString() is
- * "function objectClass () {}"
- * for IE Firefox
- */
- var arr = str.match(/function\s*(\w+)/);
- }
- if (arr && arr.length == 2) {
- return arr[1];
- }
- }
- return undefined;
- };
阅读(4347) | 评论(0) | 转发(0) |