首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >isType(obj) / getType(obj) - v0

isType(obj) / getType(obj) - v0
EN

Code Review用户
提问于 2013-03-01 15:45:26
回答 2查看 247关注 0票数 2

对于browser unknown,我的意思是,我不知道支持这件事有多久。

另外,我想知道什么时候我可以委托类型。

我听说类型更快,但下面的方法得到了更广泛的支持,并且在ES5中也有提到。

代码语言:javascript
复制
/*isType
**  dependencies - none
**  browser - unknwon
**
*/
NS.isType = function (type, o) {
    return (Object.prototype.toString.call(o).slice(8,1) === type);
};

/*getType
**  dependencies - none
**  browser - unknown
**
*/
NS.getType = function (o) {
    return (Object.prototype.toString.call(o).slice(8,1);
};

Clarification:

对检测像objects....just这样的数组不感兴趣,这是ES 5中定义的语言对象。

EN

回答 2

Code Review用户

回答已采纳

发布于 2013-03-01 18:02:51

不如:

代码语言:javascript
复制
NS.CheckType = function (o,test) {
    // implements both
    return test ? o.constructor.name === test : o.constructor.name;
};
// usage
NS.CheckType(false);       //=> 'Boolean'
NS.CheckType(false,Array); //=> false
NS.CheckType({},Object);   //=> true
NS.CheckType({},Array);    //=> false
NS.CheckType([],Object);   //=> false
NS.CheckType([],Array);    //=> true
NS.CheckType(/[a-z]/);     //=> 'RegExp'
NS.CheckType(0);           //=> 'Number'
// etc...

因为大多数js都可以从Object中“继承”,所以您也可以使用:

代码语言:javascript
复制
Object.prototype.is = function (test) {
    return test ? this.constructor === test : this.constructor.name;
};
// usage
'string'.is();        //=> 'String'
'string'.is(Object);  //=> false
 (function(){}).is(); //=> Function
 var f = function(){};
 f.is(Function);      //=> true
 // also
 function Animal(name){this.name = name || 'some animal';}
 var dog = new Animal('Bello');
 dog.is(Animal); //=> true
 // etc...

编辑在IE7-10中测试了这一点:

代码语言:javascript
复制
Object.prototype.is = function (test) {
    return test 
      ? this.constructor === test 
      : (this.constructor.name || 
         String(this.constructor)
             .match ( /^function\s*([^\s(]+)/im)[1] );
};

完整性:如果构造函数是匿名的,则该方法将失败。以下是一个解决方案:

代码语言:javascript
复制
Object.prototype.is = function (test) {
        return test 
          ? this.constructor === test 
          : (this.constructor.name || 
              ( String(this.constructor).match ( /^function\s*([^\s(]+)/im) 
               || ['','ANONYMOUS_CONSTRUCTOR'] ) [1] );
};
// usage
var Some = function(){ /* ... */}
    some = new Some;
some.is(); //=> 'ANONYMOUS_CONSTRUCTOR'

作为奖励:

代码语言:javascript
复制
Object.prototype.is = function() {
        var test = arguments.length ? [].slice.call(arguments) : null
           ,self = this.constructor;
        return test ? !!(test.filter(function(a){return a === self}).length)
               : (this.constructor.name ||
                  (String(self).match ( /^function\s*([^\s(]+)/im)
                    || [0,'ANONYMOUS_CONSTRUCTOR']) [1] );
}
// usage
var Some = function(){ /* ... */}
   ,Other = function(){ /* ... */}
   ,some = new Some;
2..is(String,Function,RegExp);        //=> false
2..is(String,Function,Number,RegExp); //=> true
some.is();                            //=> 'ANONYMOUS_CONSTRUCTOR'
some.is(Other);                       //=> false
some.is(Some);                        //=> true
// note: you can't use this for NaN (NaN === Number)
(+'ab2').is(Number);                 //=> true
票数 4
EN

Code Review用户

发布于 2013-03-01 16:07:49

我认为,在IE7和更高版本的框架中检查数组时可能会遇到问题,因此,如果您想使其在这种情况下准确,请查看这个答案在StackOverflow上

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/23317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档