根据 Crockford's,好的部分
typeof is accurate for function, string, number, boolean, and undefined. null and array are both incorrectly reported as object.
那么为什么在 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 + ']';
}; });
最佳答案
这些包装器的目的是捕获装箱的值对象。
typeof new Number(1) === 'object'
关于javascript - 为什么在某些情况下下划线不喜欢 typeof ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21470933/