Chinaunix首页 | 论坛 | 博客
  • 博客访问: 39544
  • 博文数量: 18
  • 博客积分: 366
  • 博客等级: 一等列兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-26 10:41
文章分类

全部博文(18)

文章存档

2013年(3)

2012年(15)

我的朋友

分类: JavaScript

2012-12-17 20:07:13

在javascript 中没有能够返回特定类型名的函数 
如一个对象 console.log(obj);
得到的是[object HtmlTableCellElement] 如果想要一个函数能够返回HtmlTableCellElement js中默认没有这样的函数 可以自己实现一个

下面是我实现的一个可作参考:

点击(此处)折叠或打开

  1. var getObjectClass = function (obj) {
  2.             if (obj && obj.constructor && obj.constructor.toString()) {
  3.                 
  4.                     /*
  5.                      *    for browsers which have name property in the constructor
  6.                      * of the object,such as chrome
  7.                      */
  8.                     if(obj.constructor.name) {
  9.                         return obj.constructor.name;
  10.                     }
  11.                     var str = obj.constructor.toString();
  12.                     /*
  13.                      * executed if the return of object.constructor.toString() is
  14.                      * "[object objectClass]"
  15.                      */
  16.                     
  17.                     if(str.charAt(0) == '[')
  18.                     {
  19.                             var arr = str.match(/\[\w+\s*(\w+)\]/);
  20.                     } else {
  21.                             /*
  22.                              * executed if the return of object.constructor.toString() is
  23.                              * "function objectClass () {}"
  24.                              * for IE Firefox
  25.                              */
  26.                             var arr = str.match(/function\s*(\w+)/);
  27.                     }
  28.                     if (arr && arr.length == 2) {
  29.                                 return arr[1];
  30.                             }
  31.              }
  32.              return undefined;    
  33.         };

阅读(4347) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~