__proto__ 在哪里添加到数组中?

发布于 2024-12-23 10:07:05 字数 453 浏览 0 评论 0原文

过去可以毫无问题地执行此操作:

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

现在,它突然迭代这个添加到我的 dataArray 中的 __proto__ 数组。它到底是从哪里来的?这是使用 MooTools 库造成的吗?它打破了我所有的循环! :P 现在我必须这样做:

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

我不明白的是为什么它有效...... __proto__ 实际上不算是一个数组元素吗?如果不是,那么为什么第一个版本不能像以前那样工作,而第二个版本却可以呢?

TIA 打开一些灯...

WR!

used to be able to do this without any problems:

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

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 for turning some lights on...

WR!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2024-12-30 10:07:05

不。 javascript 是原型。这意味着任何东西(任何本机类型)都可以通过修改其原型来扩展。例如,如果您想添加一个新方法,允许您迭代稍后创建的任何数组,您可以这样做:

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

这意味着创建的任何数组都将支持 .each 之后: [].each(function(el ) {});。是的,mootools 目前是一个非常典型的框架。随着mootools milk 的发展,事情正在发生变化,它是AMD 的——因此无需扩展本机...

不过,最终,在javascript 中,所有内容都通过原型链从对象本机继承。

至于数组,它并不是真正的数组类型——在 JavaScript 中,它更像是具有类似数组属性的对象。这让我明白为什么你会遇到这个问题:

for (var foo in obj)实际上是循环对象的一种方式 - 而不是数组 。它也适用于数组,因为它们有点像我提到的对象。

但这是错误的做法,特别是当您使用原型框架或不确定您运行的所有代码以及它如何影响原型链时。如果您打算使用其他语言中所谓的“关联数组”,只需使用对象即可。

在mootools中,您可以使用Object.each(function(value, key) {});来迭代它们。

或者:在循环中检查该项目是否 hasOwnProperty

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

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

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...

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

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:

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.

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.

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

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

for (var data in dataArray) {
    if (dataArray.hasOwnProperty(data)) {
        // avoids working with prototype.
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文