Underscore.js,为什么`isFunction`使用`||错误的? [英] Underscore.js, why does `isFunction` use `|| false`?

查看:38
本文介绍了Underscore.js,为什么`isFunction`使用`||错误的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆