Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore bind not work in IE8

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.

like image 518
Ashley Banks Avatar asked Jun 09 '14 11:06

Ashley Banks


1 Answers

_.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.

like image 69
lonesomeday Avatar answered Oct 31 '22 13:10

lonesomeday