简体   繁体   English

jQuery将inArray添加为最后一个数组/对象元素

[英]jQuery adds inArray as last array/object element

I have noticed that in some cases, when jQuery is imported, it adds as last element of defined object or array - inArray ... that totally ruins for ... in loop, because it counts that stupid function as element. 我注意到,在某些情况下,当导入jQuery时,它将作为已定义对象或数组的最后一个元素inArray ...完全毁坏for ... in循环,因为它将该愚蠢的函数视为元素。

Example: 例:

console.log(v);
VM19135:2 Uncaught ReferenceError: v is not defined
var v = [];
console.log(v);
VM19222:2 [inArray: function]

What the hell???? 我勒个去???? How to avoid that? 如何避免呢?

Here's an example site that gives those results: http://kobieta.interia.pl 这是提供这些结果的示例站点: http : //kobieta.interia.pl

Just enter console, and type: 只需输入控制台,然后键入:

var v = []; var v = []; console.log(v); 的console.log(V);

You need to use Object.prototype.hasOwnProperty() when you run into this problem. 遇到此问题时,您需要使用Object.prototype.hasOwnProperty() This happens because of the way JavaScript uses prototypes. 发生这种情况是因为JavaScript使用原型的方式。

It's annoying but you will need to run your for-in loop similar to this: 这很烦人,但您需要像下面这样运行for-in循环:

var v = [];
for (var name in v) {
  if (v.hasOwnProperty(name)) {
    console.log(v[name]);
  }
}

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

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