哪里__proto__被添加到阵列? [英] where does __proto__ get added to array?

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

问题描述

曾经是能够做到这一点没有任何问题:

used to be able to do this without any problems:

for (data in dataArray) { // do some stuff };

现在,它突然通过该 __迭代原__ 阵列得到了添加到我的 dataArray中。那里的地狱,它从何而来?这与使用MooTools的图书馆吗?它打破了我所有的循环! :P现在我不得不这样做,而不是:

now, it suddenly is iterating through this __proto__ array that got added to my dataArray. where the hell did it come from? is this from using the MooTools library? it's breaking all my loops! :P now i have to do this instead:

for (var i=0; i < dataArray.length; i++) { // do some stuff };

的东西我不明白就是为什么这个工程...没有 __ __原实际上并不算作一个数组元素?如果没有,那么为什么第一个版本无法正常工作像以前那样,但第二个呢?

the thing i DO NOT get is why this works... does __proto__ not actually count as an array element? if not, then why does the first version not work like it used to, but the second does?

TIA对...转向一些灯光

TIA for turning some lights on...

WR!

推荐答案

没有。 JavaScript是原型。这意味着任何东西(任何原生型)可以通过修改其原型进行扩展。例如,如果你想添加一个新方法,使您可以通过任何数组迭代以后可能创建,你会怎么做:

no. javascript is prototypical. it means that anything (any native type) can be extended by modifying its prototype. eg, if you want to add a new method that allows you to iterate through any array you may later create, you'd do:

Array.prototype.each = function(callback) {
    // this == the array. code here...
    return this;
};

这意味着创建的支持后。每任何数组: []每个(函数(EL){}); 。是的,MooTools是一个沉重的原型框架的时刻。事情与MooTools的牛奶,肚里AMD改变 - 因此不延长当地人...

this means any array created will support .each after: [].each(function(el) {});. and yes, mootools is a heavily prototypical framework AT THE MOMENT. things are changing with mootools milk, which goes AMD - hence without extending natives...

不过说到底,在JavaScript一切从通过原型链的对象本地继承。

ultimately, though, in javascript everything inherits from the Object native through the prototype chain.

为阵,它不是一个真正的正确的数组类型 - 在JavaScript中,它更像是与类数组属性的对象。这使我想到,为什么你有你有问题:

as for Array, it's not really a proper Array Type - in javascript, it's more like an Object with array-like properties. which brings me to why you have the problem you have:

为(以OBJ VAR富)实际上是循环对象的一种方式 - 而不是阵列。它可以在阵列为好,因为他们是那种喜欢的对象,因为我提及。

for (var foo in obj) is actually a way of looping Objects - not Arrays. it works on arrays as well, because they are kind of like Objects as I mentioned.

但它是错误的做法,尤其是当你有一个典型的框架工作,或者你不知道你运行code以及它如何影响了原型链。如果你的意思是使用了被称为在其他语言中的关联数组,只需使用一个对象。

but it's the wrong thing to do, especially when you work with a prototypical framework or you are not sure of all the code you run and how it has affected the prototype chain. if you mean to use what is known as an 'associative array' in other languages, simply use an Object.

在MooTools的,你可以使用 Object.each(功能(值,键){}); 来迭代他们

In mootools, you can use Object.each(function(value, key) {}); to iterate them.

或者:在循环检查,以查看该项目的的hasOwnProperty

Or: in your loop check to see if the item hasOwnProperty:

for (var data in dataArray) {
    if (dataArray.hasOwnProperty(data)) {
        // avoids working with prototype.
    }
} 

这篇关于哪里__proto__被添加到阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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