Reputation: 528
I'm using _.bind from underscore.js, however it is not working within IE8/9.
I understand MDN has a work around (MDN Polyfill - but not sure if this can be applied to the underscore library, or whether there is a fix for this in underscore itself
An example of what I'm trying to achieve is:
window.onload = _.bind(function() {
this.product.quantityListing();
}, this);
EDIT: I'm using an instance of _.bind else where and it works in IE8 - however it is just not working when I want to check the window has loaded in IE.
Upvotes: 6
Views: 593
Reputation: 173572
The whole premise of Underscore is that it does work for IE8 as well as other browsers, but the way in which you're using it is highly unusual if not plain wrong. You would use it like so:
window.onload = _.bind(function() {
this.product.quantityListing();
}, this);
I.e. without the new
keyword.
The result of _.bind()
is a closure onto which this
is bound; once the document is done loading it will call the function with the expected context.
Upvotes: 0
Reputation: 237885
_.bind
and the Function#bind
shim from MDN do essentially the same thing. If you use the MDN method, you need not use the Underscore.js method.
You would use the MDN method like this:
window.onload = (function() {
this.product.quantityListing();
}).bind(this);
On the other hand, if you use the MDN shim before you include Underscore in your page, Underscore will use the shimmed version if necessary.
So if you include the shim before Underscore, you can use whichever you prefer. Personally I'd stick with using Function#bind
, because it has (very slightly) better performance in browsers that natively support it.
Upvotes: 3