Javascript RegExp的fn实例
我盯着angular源代码(就像我不时做的那样),我惊讶地看到以下检查:Javascript RegExp的fn实例,javascript,angularjs,Javascript,Angularjs,我盯着angular源代码(就像我不时做的那样),我惊讶地看到以下检查: if (isFunction(fn) && !(fn instanceof RegExp)) { } else { // in IE, native methods are not functions so they cannot be bound (note: they don't need to be) } 它是angular.bind实现的一部分。可以找到完整的代码片段 这是isFunctio
if (isFunction(fn) && !(fn instanceof RegExp)) {
} else {
// in IE, native methods are not functions so they cannot be bound (note: they don't need to be)
}
它是angular.bind实现的一部分。可以找到完整的代码片段
这是isFunction
的实现(如果您想知道):
现在,让我补充一下,以防万一:
我知道操作员的实例是什么。它检查函数的原型(在本例中为RegExp.prototype
)是否可以在从特定对象开始的原型链上找到
现在,我的问题是:
你能给我一个例子,上面的代码将遵循else子句吗?我对表达式的第二部分感兴趣,我知道不提供函数可以使条件失败
这条评论警告我IE中有一种奇怪的行为,但我无法复制它。在IE7中:
typeof window.item // => 'string'
但是(在IE7中):
在IE9中:
typeof window.item // => 'function'
在IE11中:
typeof window.item // => 'string'
在Netscape最初的实现中,RegExp
对象实际上是函数re(string)
和re.exec(string)
的意思应该是一样的。在Netscape和Mozilla浏览器中,它通过Firefox4(2011年3月22日发布)得到支持。但是,typeof
甚至更早地停止了对RegExp
对象返回“函数”
这不是第一次在《天使法典》中注意到黑暗时代的遗产。这是。Angular 1.0.0于2012年6月13日发布。我有一个模糊的记忆(可能只有在IE中)RegExp实例可以作为函数调用。不过,我在谷歌上找不到任何相关信息。typeof window.item
在IE11上生成“函数”(至少在我的机器上)。此外,对于表达式fn instanceof RegExp
typeof window.item // => 'function'
typeof window.item // => 'string'