如何为Internet Explorer浏览器修复JavaScript中的数组indexOf()

本文翻译自:How to fix Array indexOf() in JavaScript for Internet Explorer browsers

If you have worked with JavaScript at any length you are aware that Internet Explorer does not implement the ECMAScript function for Array.prototype.indexOf() [including Internet Explorer 8]. 如果您曾经使用过JavaScript,那么您就会知道Internet Explorer不会为Array.prototype.indexOf()[包括Internet Explorer 8]实现ECMAScript函数。 It is not a huge problem, because you can extend the functionality on your page with the following code. 这不是一个大问题,因为您可以使用以下代码扩展页面上的功能。

Array.prototype.indexOf = function(obj, start) {
     for (var i = (start || 0), j = this.length; i < j; i++) {
         if (this[i] === obj) { return i; }
     }
     return -1;
}

When should I implement this? 我应该何时实施?

Should I wrap it on all my pages with the following check, which checks if the prototype function exists and if not, go ahead and extend the Array prototype? 我是否应该使用以下检查将其包装在我的所有页面上,该检查将检查原型函数是否存在,如果不存在,请继续并扩展Array原型?

if (!Array.prototype.indexOf) {

    // Implement function here

}

Or do browser check and if it is Internet Explorer then just implement it? 还是要检查浏览器,如果它是Internet Explorer,则只需实施它?

//Pseudo-code

if (browser == IE Style Browser) {

     // Implement function here

}

#1楼

参考:https://stackoom.com/question/7Jm2/如何为Internet-Explorer浏览器修复JavaScript中的数组indexOf


#2楼

underscore.js库具有indexOf函数,您可以改用它:

_.indexOf([1, 2, 3], 2)

#3楼

I would recommend this to anyone looking for missing functionality: 我会推荐给任何缺少功能的人:

http://code.google.com/p/ddr-ecma5/ http://code.google.com/p/ddr-ecma5/

It brings in most of the missing ecma5 functionality to older browers :) 它为老式浏览器带来了大多数缺少的ecma5功能:)


#4楼

Do it like this... 像这样做...

if (!Array.prototype.indexOf) {

}

As recommended compatibility by MDC . MDC建议的兼容性

In general, browser detection code is a big no-no. 通常,浏览器检测代码是很大的禁忌。


#5楼

You should check if it's not defined using if (!Array.prototype.indexOf) . 您应该检查是否未使用if (!Array.prototype.indexOf)定义。

Also, your implementation of indexOf is not correct. 另外,您对indexOf的实现也不正确。 You must use === instead of == in your if (this[i] == obj) statement, otherwise [4,"5"].indexOf(5) would be 1 according to your implementation, which is incorrect. 您必须在if (this[i] == obj)语句中使用===而不是== ,否则根据您的实现, [4,"5"].indexOf(5)将为1,这是不正确的。

I recommend you use the implementation on MDC . 我建议您在MDC上使用实现


#6楼

This was my implementation. 这是我的实现。 Essentially, add this before any other scripts on the page. 本质上,在页面上的任何其他脚本之前添加此代码。 ie in your master for a global solution for Internet Explorer 8. I also added in the trim function which seems to be used in allot of frameworks. 例如,在您的Internet Explorer 8全局解决方案管理员中。我还添加了trim函数,该函数似乎在框架分配中使用。

<!--[if lte IE 8]>
<script>
    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(obj, start) {
            for (var i = (start || 0), j = this.length; i < j; i++) {
                if (this[i] === obj) {
                    return i;
                }
            }
            return -1;
        };
    }

    if(typeof String.prototype.trim !== 'function') {
        String.prototype.trim = function() {
            return this.replace(/^\s+|\s+$/g, '');
        };
    };
</script>
<![endif]-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值