Binding underline does not work in IE8

I use _.bind from underscore.js, however it does not work in IE8 / 9.

I understand that MDN works ( Polyfill MDN - but not sure if this can be applied to the underscore library, or is there a fix for this in the underscore itself

An example of what I'm trying to achieve:

window.onload = _.bind(function() { this.product.quantityListing(); }, this); 

EDIT: I use an instance of _.bind else where it works in IE8 - however it just doesn't work when I want to check that the window has loaded in IE.

+6
source share
2 answers

_.bind and Function#bind padding from MDN do essentially the same thing. If you use the MDN method, you do not need to use the Underscore.js method.

You should use the MDN method as follows:

 window.onload = (function() { this.product.quantityListing(); }).bind(this); 

On the other hand, if you use the MDN slot, before including Underscore on your page, Underscore will use the slotted version if necessary.

So, if you turn on the pad before Underscore, you can use what you prefer. Personally, I stick to using Function#bind because it has (very little) better performance in browsers that support it.

+3
source

The whole premise of Underscore is that it works for both IE8 and other browsers, but the way you use it is very unusual, if not mistaken. You would use it like this:

 window.onload = _.bind(function() { this.product.quantityListing(); }, this); 

those. without the keyword new .

The result of _.bind() is the closure this is bound to; After the document is loaded, it will call a function with the expected context.

0
source

Source: https://habr.com/ru/post/970565/


All Articles