我正盯着角度源代码(就像我不时那样),我很惊讶地看到以下检查:
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
实施的一部分。完整的代码段可以找到here。
这是isFunction
的实现(如果你想知道的话):
function isFunction(value) {return typeof value === 'function';}
现在,让我添加一下,以防万一:
我知道instanceof
运算符的作用。它检查是否可以从特定对象开始在原型链上找到函数的原型(在本例中为RegExp.prototype
)。
现在,我的问题是:
您能举例说明上述代码将遵循else子句的情况吗?我对表达式的第二部分感兴趣,我知道你可以通过不提供函数来使条件失败。
该评论警告我IE中的奇怪行为,但我无法重现它。
答案 0 :(得分:1)
在IE7中:
typeof window.item // => 'string'
然而(在IE7中):
window.item(0) // => [object] { onbeforeunload: null, ...}
在IE9中:
typeof window.item // => 'function'
在IE11中:
typeof window.item // => 'string'
答案 1 :(得分:0)
在Netscape的原始实现中,RegExp
对象实际上是函数。 re(string)
和re.exec(string)
应该是相同的意思。在Netscape和Mozilla浏览器中,它通过Firefox 4(2011年3月22日发布)得到支持。但是,typeof
甚至更早就停止了"function"
个RegExp
对象的返回。
这不是第一次在Angular的代码中注意到黑暗时代的遗产。这是another example。 Angular 1.0.0于2012年6月13日发布。