迭代与JavaScript数组的"在"关键词 [英] Iterating on Javascript arrays with the "in" keyword

查看:100
本文介绍了迭代与JavaScript数组的"在"关键词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,我不明白的在JavaScript中关键字的含义。

It seems that I don't understand the meaning of the in keyword in JavaScript.

看一看这个code段( http://jsfiddle.net/3LPZq/ ):

Have a look at this code snippet ( http://jsfiddle.net/3LPZq/ ):

var x = [1,2]
for(i in x){
    document.write(x[i]);
}

当在运行的jsfiddle,它打印不仅包含在数组中的值,而且所有的数组对象的属性和方法。

When run in the jsfiddle, it prints not only the values contained in the array, but also all of the array object's properties and methods.

当我改变它像这样( http://jsfiddle.net/4abmt/ ):

When I change it like this ( http://jsfiddle.net/4abmt/ ):

$(document).ready(function(){
var x = [1,2]
for(i in x){
    document.write(x[i]);
}});

它打印只值1和2。

it prints only the values 1 and 2.

为什么会出现这种情况?难道这或造成jQuery也依赖于该文件是否是完全加载与否的关键字的行为?

Why does this happen? Is this caused by jQuery or does the behavior of the in keyword depend on whether the document is fully loaded or not?

推荐答案

通过一些加载的jsfiddle的库扩展阵列的默认原型,因此,它显示了这些额外的功能。 jQuery的不扩展Array原型,所以你看不到任何多余的功能。

Some of the libraries loaded by jsFiddle extend the default prototype of Array, thus it shows these extra functions. jQuery doesn't extend the Array prototype and so you don't see any extra functions.

这篇关于迭代与JavaScript数组的"在"关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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