Razão por trás do uso de 'instanceof function () {}'?
No Mozilla Developer Center, há uma página sobre oFunction.prototype.bind
e fornece uma função de compatibilidade para navegadores que não suportam essa funçã
No entanto, ao analisar este código de compatibilidade, não consigo descobrir por que eles usaminstanceof nop
. nop
foi definido comofunction() {}
. Qual parte da especificação ECMA embind
isso corresponde a? E quais variáveis são uma instância defunction() {}
?
Os seguintes retornosfalse
, então não sei completamente para que serve. O que as coisas retornam verdade ao fazer uminstanceof function() {}
Verifica
(function() {}) instanceof (function() {}) // false
O código é o seguinte:
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;
};