admin管理员组文章数量:1356709
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.
本文标签: javascriptwhy is typeof ()39function39 used in underscoreStack Overflow
版权声明:本文标题:javascript - why is typeof (.) !== 'function' used in underscore - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744064753a2584780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论