简体   繁体   English

下划线绑定在IE8中不起作用

[英]Underscore bind not work in IE8

I'm using _.bind from underscore.js, however it is not working within IE8/9. 我正在使用来自underscore.js的_.bind,但是它在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 我理解MDN有一个解决方法( MDN Polyfill - 但不确定这是否可以应用于下划线库,或者是否在下划线本身中有此修复

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. 编辑:我正在使用_.bind其他地方的实例,它在IE8中工作 - 但是当我想检查窗口已加载到IE时,它只是不工作。

_.bind and the Function#bind shim from MDN do essentially the same thing. _.bind和来自MDN的Function#bind shim基本上做同样的事情。 If you use the MDN method, you need not use the Underscore.js method. 如果使用MDN方法,则无需使用Underscore.js方法。

You would use the MDN method like this: 你会像这样使用MDN方法:

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. 另一方面,如果在页面中包含Underscore之前使用MDN垫片,则Underscore将在必要时使用垫片版本。

So if you include the shim before Underscore, you can use whichever you prefer. 因此,如果你在Underscore之前加入垫片,你可以使用你喜欢的任何一个。 Personally I'd stick with using Function#bind , because it has (very slightly) better performance in browsers that natively support it. 就个人而言,我坚持使用Function#bind ,因为它在本机支持它的浏览器中具有(非常轻微)更好的性能。

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. Underscore的整个前提是它确实适用于IE8以及其他浏览器,但是如果不是完全错误的话,你使用它的方式是非常不寻常的。 You would use it like so: 你会像这样使用它:

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

Ie without the new keyword. 即没有new关键字。

The result of _.bind() is a closure onto which this is bound; 的结果_.bind()是一个封闭其上this是结合; once the document is done loading it will call the function with the expected context. 一旦文档完成加载,它将调用具有预期上下文的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM