I was reading through the source for the _.isFunction()
function and saw this line:
if (typeof (/./) !== 'function') {
and I don't understand why it's there. /./
is a regex that always seem to have the type object
. Why wouldn't _.isFunction
be redefined if /./
type was a function
?
I was reading through the source for the _.isFunction()
function and saw this line:
if (typeof (/./) !== 'function') {
and I don't understand why it's there. /./
is a regex that always seem to have the type object
. Why wouldn't _.isFunction
be redefined if /./
type was a function
?
- 4 Answered here: stackoverflow./questions/5054352/… – Dogbert Commented Jul 11, 2013 at 17:49
- Ah, so the check is to avoid accidentally having regex considered function? Nice. Excellent question. – ZenMaster Commented Jul 11, 2013 at 17:52
- 3 "Why wouldn't _.isFunction be defined" - it's being redefined. The original toString() definition is two blocks up. – Rup Commented Jul 11, 2013 at 17:52
1 Answer
Reset to default 16Some versions of various JavaScript engines have allowed for calling RegExp
as another way of using .exec()
:
var pattern = /./;
pattern('abc');
pattern.exec('abc');
And, since they were Callable, typeof
considered them function
s:
Type of val: Object (native or host and does implement [[Call]])
Result:"function"
To my knowledge, though, current versions don't exhibit this behavior and will throw a TypeError
. But, if you're concerned with backwards patibility, as Underscore is, you may need to check for it.