【发布时间】:2014-01-31 01:26:44
【问题描述】:
每个 Crockford 的,好的部分
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 + ']';
}; });
【问题讨论】:
-
我认为除了可读性和更短的代码之外没有其他真正的原因。如果您想要性能,请查看 Lodash。