Array.prototype.indexOf被添加到结束IE8阵列 [英] Array.prototype.indexOf being added to end of arrays in IE8

查看:99
本文介绍了Array.prototype.indexOf被添加到结束IE8阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道IE8和更早版本不具有的indexOf功能。我将其定义如下:

I know IE8 and earlier doesn't have an indexOf function. I'm defining it as follows:

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;
    }
}

我可以得到正确值的指数在一个数组,但使用IE8和更早的时候功能被添加到我的数组末端。因此,我得到这样的话:

I can correctly get the index of values in an array, but the function is being added to the end of my arrays when using IE8 and earlier. Therefore, I'm getting things like:

obj.obj2[0] = 'data'
obj.obj2[1] = 'other data'
obj.obj2['indexOf'] = [definition of indexOf function]

这并不奇怪,这是打破一切都在网站上其他人。问题不是出在IE10发生或9任何及所有帮助AP preciated。

Not surprisingly, this is breaking everything else on the site. Problem isn't happening in IE10 or 9. Any and all help is appreciated.

推荐答案

它添加到原型,所以每次你把你的阵列像一个对象(的for..in 循环就是一个例子),它会显示出来。它不会在其他浏览器中显示出来,因为他们已经拥有默认情况下,的indexOf 方法,所以你不修改的原型。

It is added to the prototype, so everytime you treat your array like an object (for..in loop is one example), it will show up. It does not show up in other browsers because they already have the indexOf method by default, so you're not modifying the prototype.

您可以使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty\"相对=nofollow> obj.hasOwnProperty(propertyName的) 来测试属性是否被直接定义你的对象(在这种情况下,你的阵列,它是一个对象基本)或其他地方的原型链。

You can use obj.hasOwnProperty(propertyName) to test whether a property is defined directly on your object (in this case, your array, which is an object basically) or somewhere else in the prototype chain.

这篇关于Array.prototype.indexOf被添加到结束IE8阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆