When binding a function to the same context, the resulting reference is different every time.
function foobar() {
return 1;
}
var foo = foobar.bind(this);
var bar = foobar.bind(this);
console.log(foo === bar); // Nope
Yes the Function.prototype.bind() creates a new function every time.
Does that code copy the function every time?
Whether or not it copies the original function is an implementation detail of the underlying JS engine.
The return value of bind
is a new function, and you'll get a new one each time you call bind
.
Wouldn't there be any benefit in caching that behavior?
There might be a mild performance gain to be had in caching the resulting function, but probably not enough to worry about unless you are generating hundreds of bound copies of the same function. Beware of premature optimization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With