I'm using Ruby at daily basis, but Javascript is nowadays everywhere, and I need to learn this language too.
I began with "Learning JavaScript Design Patterns", now I read the 6th edition of "JavaScript: The Definitive Guide".
I'm reading some blogs too.
I found a code snippet, which I totally not understood:
if (!Function.prototype.bind) {
Function.prototype.bind = function(obj) {
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)));
};
nop.prototype = self.prototype;
bound.prototype = new nop();
return bound;
};
}
I know it checks if a function with the name bind is defined, and when not, define it.
But why it is checked on the prototype of the Function object ?
Why it is not a simple check of:
if(typeof bind != 'function')