简体   繁体   English

数组中的javascript原型循环

[英]javascript prototype loop for in array

I am working on a project, which use a lot of "javascript". 我正在研究一个使用大量“javascript”的项目。 I have lot of "ajax" calls, which returns always à "json" array. 我有很多“ajax”调用,它总是返回“json”数组。 For usefull reasons, I created two prototypes of Array object ("in_array" function and "shuffle" function). 由于有用的原因,我创建了两个Array对象的原型(“in_array”函数和“shuffle”函数)。

Since I did the prototypes, all my "for in" loops are broken (two times showing "undefined" ...) I looked into google and stackoverflow, and I know my mistake now. 由于我做了原型,我的所有“for in”循环都被打破了(两次显示“未定义”......)我调查了谷歌和stackoverflow,我现在知道我的错误。 Of course, if I delete my prototypes, the "undefined" go away. 当然,如果我删除我的原型,“undefined”就会消失。

But, I don't find all my answers. 但是,我没有找到我所有的答案。

First, I see lot of times, that using "for in" loops are bad, why ? 首先,我看到很多次,使用“for in”循环是不好的,为什么?

I develop in "PHP" or "python" too, and I DO love "for in" or "foreach" loops. 我在“PHP”或“蟒蛇”开发过,我爱“中”或“的foreach”循环。

Secondly, I got a really lot of "for in" loops, so honnestly I prefer changing my prototypes, than changing my loops. 其次,我得到了很多“for in”循环,所以我更喜欢改变我的原型,而不是改变我的循环。 Is this that much dirty to switch my prototypes to normal functions, and keep "for in" loops ? 将我的原型切换到正常函数并保持“for in”循环是否非常脏?

Thirdly, I read that JQuery can correct this bug. 第三,我读到JQuery可以纠正这个错误。 My project did got JQuery, what is solution talking about (I only read that JQuery can correct this, not how). 我的项目确实得到了JQuery,解决方案是什么(我只读了JQuery可以纠正这个,而不是如何)。

Thank you, 谢谢,

EDIT : My prototypes code : 编辑:我的原型代码:

// FONCTIONS ARRAY
Array.prototype.shuffle = function () {
for (var i = this.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var tmp = this[i];
    this[i] = this[j];
    this[j] = tmp;
}

return this;
}

Array.prototype.in_array = function (needle) {
    for(var i = 0; i < this.length; i++) {
       if(this[i] == needle)
           return true;
    }
    return false;
}

Here is what a map function could look like. 这是地图功能的样子。 I wrote this out of the top of my head so there may be bugs. 我把它写在了我的头顶,所以可能有错误。

function object_map(object, callback) {
    var key, ret = {};
    for (key in object) {
        if (Object.prototype.hasOwnProperty.call(object, key)) {
            ret[key] = callback(key, object[key]);
        }
    }
    return ret;
}

Your problem occurs: 你的问题出现了:

  1. because you're adding (enumerable) functions to Array.prototype which will then appear any time you enumerate any array. 因为你要向Array.prototype添加(可枚举的)函数,这些函数会在你枚举任何数组时出现。

  2. because you shouldn't use for ... in to enumerate arrays, only for enumerating object keys. 因为你不应该使用for ... in来枚举数组,仅用于枚举对象键。

If you know you're running on ES5 browsers, you can safely add functions to Array.prototype using Object.defineProperty : 如果您知道自己在ES5浏览器上运行,则可以使用Object.defineProperty安全地向Array.prototype添加函数:

Object.defineProperty(Array.prototype, 'shuffle', {
    value: function() {
        ...
    }
});

which defaults to creating non-enumerable properties. 默认为创建非可枚举属性。

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

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