`

javascript中for/in的用法

阅读更多

 

 

 

6.9. for/in

The for keyword is used in two ways in JavaScript. You've just seen how it is used in the for loop. It is also used in the for/in statement. This statement is a somewhat different kind of loop with the following syntax:

for (variable in object)
    statement

 

variable should be either the name of a variable, a var statement declaring a variable, an element of an array, or a property of an object (i.e., it should be something suitable as the left side of an assignment expression). object is the name of an object or an expression that evaluates to an object. As usual,statement is the statement or statement block that forms the body of the loop.

You can loop through the elements of an array by simply incrementing an index variable each time through a while or for loop. The for/in statement provides a way to loop through the properties of an object. The body of the for/in loop is executed once for each property of object. Before the body of the loop is executed, the name of one of the object's properties is assigned to variable, as a string. Within the body of the loop, you can use this variable to look up the value of the object's property with the []operator. For example, the following for/in loop prints the name and value of each property of an object:

for (var prop in my_object) {
    document.write("name: " + prop + "; value: " + my_object[prop], "<br>");
}

 

Note that the variable in the for/in loop may be an arbitrary expression, as long as it evaluates to something suitable for the left side of an assignment. This expression is evaluated each time through the loop, which means that it may evaluate differently each time. For example, you can use code like the following to copy the names of all object properties into an array:

var o = {x:1, y:2, z:3};
var a = new Array( );
var i = 0;
for(a[i++] in o) /* empty loop body */;

 

JavaScript arrays are simply a specialized kind of object. Therefore, the for/in loop enumerates array indexes as well as object properties. For example, following the previous code block with this line enumerates the array "properties" 0, 1, and 2:

for(i in a) alert(i);

 

The for/in loop does not specify the order in which the properties of an object are assigned to the variable. There is no way to tell what the order will be in advance, and the behavior may differ among implementations or versions of JavaScript. If the body of a for/in loop deletes a property that has not yet been enumerated, that property will not be enumerated. If the body of the loop defines new properties, whether or not those properties will be enumerated by the loop is implementation-dependent.

The for/in loop does not actually loop through all possible properties of all objects. In the same way that some object properties are flagged to be read-only or permanent (nondeletable), certain properties are flagged to be nonenumerable. These properties are not enumerated by the for/in loop. While all user-defined properties are enumerated, many built-in properties, including all built-in methods, are not enumerated. As you'll see in Chapter 7, objects can inherit properties from other objects. Inherited properties that are user-defined are also enumerated by the for/in loop.

 

 

 

 

6.9. for/in

 

for关键字在javascript中有两种用法,在for循环里,你已经看到了他是如何使用的,for也可以使用在for/in语句里,下面的语句稍微不同于循环,

 

for (variable in object)
    statement

 

 

variable可以是以下中的任何一个,命名变量、var关键字声明的变量、数组的一个元素或者对象的一个属性(即其必须是一个适当的、可赋值的左值),object是命名对象或者是可返回对象的表达式,通常,statement是声明语句或构成循环体的语句块。

 

你可以使用while或者for循环简单的通过递增索引的方式迭代任意数组元素。而for/in循环提供了一个迭代对象属性的方法,object的每个属性for/in循环体都执行一次迭代,在循环体执行前,对象的属性名字作为string类型付给了变量variable。在循环体内,可以使用这个变量通过[]操作符访问对象的属性值,例如,下面的for/in循环打印了一个对象每一个属性的名字和属性值

 

for (var prop in my_object) {
    document.write("name: " + prop + "; value: " + my_object[prop], "<br>");
}

 

注意:在for/in中的variable变量可以是任意表达式,只要执行结果是可赋值的左值即可.这个表达式循环时每次都会执行,意思是说:每次执行的都不同。例如,编写如下代码拷贝对象的所有属性到数组里:

 

var o = {x:1, y:2, z:3};
var a = new Array( );
var i = 0;
for(a[i++] in o) /* empty loop body */;

 

其实,javascript数组也只不过是一个特殊的对象而已,因此,for/in循环迭代数组索引和循环迭代对象属性是一样的,例如,上面代码块再加上这句迭代输出数组属性值为0,1,2

 

for(i in a) alert(i);

 

 

for/in循环不能指定赋值给variable变量的对象属性的顺序,无法指定哪一个将在前面,在不同的javascript版本中,这个行为也是不同的。如果for/in循环体内删除了一个尚未迭代的属性那么这个属性不会被迭代,如果在循环体内添加了属性,那么这些属性是否被迭代要视具体实现了

 

for/in循环实际上不是万能的,它并不能迭代所有对象的所有属性,有些特殊情况下,例如一些对象的属性标示为只读或者不可变的,标示为不可迭代的。这些属性不能被for/in迭代。所有用户自定义属性是可以迭代的,许多内建属性包括所有内建方法是不可以被迭代的。在第七章,你将看到对象是可以继承其他对象的属性的,继承而来的属性是用户自定义属性,是可以用for/in迭代的

 

 

 

 

 

 

 

 

 

 

0
1
分享到:
评论

相关推荐

    javascript中for/in循环及使用技巧

    本篇文章将详细介绍JavaScript中for/in循环的基本语法、使用场景以及一些实用技巧。 首先,我们需要理解for/in循环的基本语法。for/in循环的语法结构如下: ```javascript for (variable in object) statement; `...

    JavaScript中“过于”犀利地for/in循环使用示例

    在JavaScript编程语言中,`for/in`循环是一种特殊类型的循环,用于遍历对象的属性,而不仅仅是数组的索引。这个循环结构对于处理对象的属性非常有用,但如果不小心使用,可能会导致一些意外的结果,就像在提供的示例...

    Javascript中For In语句用法实例

    这是因为,JavaScript 的数组是以字符串为键的关联数组,而对象属性名(索引)本身在 JavaScript 中也是字符串类型,因此当使用“for...in”来遍历数组时,遍历出来的将不仅仅只是数值类型的索引,还可能包括一些非...

    JavaScript中for-in遍历方式示例介绍

    在实际开发中,我们应谨慎使用`for-in`循环,并结合`hasOwnProperty()`等方法来确保遍历的准确性。此外,遍历数组通常更推荐使用`for`循环或者`Array.prototype.forEach()`这样的迭代方法,以避免意外遍历到原型链上...

    全面解析JavaScript里的循环方法之forEach,for-in,for-of

    在JavaScript中,有三种主要的循环方法:`forEach`、`for-in`和`for-of`,每种都有其特定的用途和特点。 `forEach`方法是ES5引入的一个数组方法,简化了对数组元素的遍历。例如: ```javascript myArray.forEach...

    【JavaScript源代码】JS中for,for...in,for...of和forEach的区别和用法实例.docx

    本文主要介绍四种常用的循环结构:`for`循环、`for...in`循环、`for...of`循环以及`Array.prototype.forEach()`方法,并通过具体的示例来探讨它们之间的区别。 #### 一、for循环 `for`循环是最常见的循环结构之一...

    javascript中的遍历for in 以及with的用法

    for in 循环执行一个对象中的属性 with语句: (对象操作语句)  功能:为一段程序建立默认对象  格式: 代码如下:  with(&lt;对象&gt;){  &lt;语句组&gt;  } 具体示例: 代码如下: [removed] function member(name,...

    js中for in的用法示例解析.docx

    在JavaScript中,`for...in`循环是一种遍历对象属性的迭代语句,它主要用于枚举对象的所有可枚举属性,包括从原型链继承而来的属性。以下是对`for...in`循环的详细解释和使用示例。 首先,让我们看一个简单的`for.....

    [JavaScript进阶]Professional JavaScript for Web Developers(3rd)

    25. 新兴APIs(Chapter 25: Emerging APIs):可能介绍了一些新兴的、尚未广泛支持的JavaScript API,以及它们的使用方法和未来趋势。 附录部分: A. ECMAScript Harmony:描述了ECMAScript标准的更新,也称为...

    JavaScript Intellisense for Ext 2.0.1 in Visual Studio 2008

    总的来说,JavaScript Intellisense for Ext 2.0.1 in Visual Studio 2008是提高开发效率和减少出错可能性的重要工具。它让开发者在不离开IDE的情况下就能充分利用Ext的功能,提升了开发体验。同时,这也体现了...

    讲解JavaScript中for...in语句的使用方法

    JavaScript中的for...in语句是一种专门用来遍历对象属性的循环结构。在JavaScript编程中,对象可以看作是一个属性集合,通常包括方法和数据。而for...in循环提供了一种方便的方式来访问这些属性。 在介绍for...in...

    讲解JavaScript中for…in语句的使用方法

    它被称为for…in循环。这个循环是用于循环一个对象的属性。 因为我们还没有讨论的对象,所以使用这一循环可能会感觉不太明白。但是,一旦你会对JavaScript对象了解后,那么会发现这个循环非常有用。 语法 for ...

    Javascript中的for in循环和hasOwnProperty结合使用

    在JavaScript编程中,`for...in`循环和`hasOwnProperty`方法是两个非常重要的概念,它们主要用于遍历和检查对象的属性。`for...in`循环遍历的是对象及其原型链上的可枚举属性,而`hasOwnProperty`方法则用于确定对象...

    JavaScript for Data Science .pdf

    《JavaScript for Data Science》是一本关于如何使用JavaScript进行数据科学工作的专业书籍。该书属于Chapman & Hall/CRC数据科学系列,这一系列反映了数据科学领域的跨学科特性,汇集了来自统计学、计算机科学、...

    JavaScript For…In 使用方法

    JavaScript For…In 声明 For…In 声明用于对数组或者对象的属性进行循环操作。 for … in循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作。 语法: for (变量 in 对象) { 在此执行...

    JavaScript中关键字 in 的使用方法详解

    在本文中,我们将详细解析关键字"in"的使用方法。 首先,关键字"in"最常用于for-in循环中,for-in循环通常用于遍历对象的属性。不同于for循环,for-in循环会遍历对象的所有可枚举属性,包括那些通过原型链继承来的...

Global site tag (gtag.js) - Google Analytics