Underscore.js why `isFunction` uses` || false`?

An additional override for isFunction(object) in Underscore.js ( repo reference to definition ) is 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; }; } 

What bothers me is || false || false , why is this necessary after string comparison? Since typeof always returns a string, shouldn't there be any ambiguity?
The comment says that overriding fixes some typeof errors, are there cases on the listed platforms when typeof doesn't return a string?

+7
javascript
source share
1 answer

See the questions described in the comments, # 1621 , # 1929 and # 2236 .

In short, there is an error on some platforms where typeof not a string unless you store it in a variable.
|| false || false fixes the problem without introducing an additional variable.

Taken directly from # 1621 :

In IE8, with a variable, everything works as expected:

 var t = typeof obj t === 'function' // false t === 'object' // true 

but without it, everything is different:

 (typeof obj) === 'function' // true, but SHOULD be false (typeof obj) === 'object' // true 

The additional check described above corrects the error.

+6
source share

All Articles