为什么不强调在某些情况下更喜欢typeof?

时间:2014-01-31 01:26:45

标签: javascript underscore.js

Per Crockford's,The Good Parts

  

typeof对于函数,字符串,数字,布尔值和   未定义。 null和数组都被错误地报告为对象。

那么为什么不在它运行的情况下使用typeof?它总是更快,这就是为什么我想知道为什么在这些情况下它不是首选。

特别是这里的功能,字符串和数字。

http://underscorejs.org/docs/underscore.html#section-111

  each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
    _['is' + name] = function(obj) {
      return toString.call(obj) == '[object ' + name + ']';
    };   });

1 个答案:

答案 0 :(得分:2)

这些包装器的目的是捕获盒装值对象。

typeof new Number(1) === 'object'

相关问题