Underscore.js,为什么`isFunction`使用`||错误的? [英] Underscore.js, why does `isFunction` use `|| false`?
问题描述
Underscore.js中 isFunction(object)
的可选替代(指向定义的repo链接),内容如下:
The optional override for isFunction(object)
in Underscore.js (repo link to definition), reads as follows:
// Optimize `isFunction` if appropriate. Work around some typeof bugs in old v8,
// IE 11 (#1621), Safari 8 (#1929), and PhantomJS (#2236).
var nodelist = root.document && root.document.childNodes;
if (typeof /./ != 'function' && typeof Int8Array != 'object' && typeof nodelist != 'function') {
_.isFunction = function(obj) {
return typeof obj == 'function' || false;
};
}
我很困惑的是 ||false
,为什么在进行字符串比较后有必要?由于 typeof
始终在此处返回字符串应该没有歧义吗?
该评论指出覆盖覆盖修复了一些 typeof
错误,在列出的平台上是否存在 typeof
不返回字符串的情况?
What I'm confused about is the || false
, why is it necessary after a string comparison? Since typeof
always returns a string there should be no ambiguity?
The comment states the override fixes some typeof
bugs, are there cases on the listed platforms when typeof
doesn't return a string?
推荐答案
请参阅注释#1621 ,#1929 和简而言之,某些平台存在一个错误,其中 typeof
不是字符串,除非将其存储在变量中. ||false
可解决此问题,而无需引入额外的变量.
Shortly put, some platforms have a bug where typeof
isn't a string unless you store it in a variable.
The || false
fixes the issue without introducing an extra variable.
直接从#1621 拍摄:
在IE8中,使用变量可以按预期工作:
In IE8, with a variable everything works as expected:
var t = typeof obj
t === 'function' // false
t === 'object' // true
但没有一个,情况就不同了:
but without one, things are different:
(typeof obj) === 'function' // true, but SHOULD be false
(typeof obj) === 'object' // true
上面概述的额外检查修复了该错误.
The extra check outlined above fixes the bug.
这篇关于Underscore.js,为什么`isFunction`使用`||错误的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!