在Mozilla開發人員中心,有一個關於Function.prototype.bind
函數的頁面,併爲不支持此功能的瀏覽器提供兼容性功能。使用'instanceof function(){}'的原因?
但是,當分析此兼容性代碼時,我找不到爲什麼他們使用instanceof nop
。 nop
已被設置爲function() {}
。這與bind
ECMA規範的哪一部分相符?哪些變量是function() {}
的實例?
以下返回false
,所以我不完全知道它用於什麼。在做檢查時,什麼事情返回true?
(function() {}) instanceof (function() {}) // false
的代碼如下:
Function.prototype.bind = function(obj) {
if(typeof this !== 'function')
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
var slice = [].slice,
args = slice.call(arguments, 1),
self = this,
nop = function() {},
bound = function() {
return self.apply(this instanceof nop ? this : (obj || {}),
args.concat(slice.call(arguments)));
};
bound.prototype = this.prototype;
return bound;
};