Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

for each...in

This translation is incomplete. Please help translate this article from English.

טענת "for each...in" הוצאה משימוש בסטנדרט (ECMA-357 (E4X. התמיכה ב-E4X אמנם הוסרה, אך טענת ה"for each...in" לא תבוטל ולא תוסר מטעמי תאימות לגרסאות קודמות. עם זאת, מומלץ להשתמש בטענת "for...of" במקום. (ע"ע bug 791343 ). 

טענה מסוג:

for each...in

("עבור כל... ב...")

חגה סביב  כל הערכים של כל "פריטי הרכוש" (properties) של משתנה נתון. עבור כל אחד מפריטי הרכוש תבוצע טענה מוגדרת.

תחביר

for each (variable in object) {
  statement
}
variable
המשתנה שיחוג סביב הערכים של פרטי הרכוש. ניתן (אך לא חובה)  להצהיר על משתנה זה עם מילת המפתח var. המשתנה הזה הוא פנימי לפונקציה, ולא ללולאה עצמה.
object
האובייקט שסביב פריטי הרכוש שלו יש לחוג. 
statement
טענה שיש לבצע עבור כל אחד מפריטי הרכוש. על מנת לבצע יותר מטענה אחת בתוך הלולאה, יש להשתמש בטענת בלוק ({ ... }) כדי לקבץ את הטענות הללו יחדיו.

תיאור

 

Some built-in properties are not iterated over. These include all built-in methods of objects, e.g. String's indexf

method. However, all user-defined properties are iterated over.

Examples

Using for each...in

Warning: Never use a loop like this on arrays. Only use it on objects. See for...in for more details.

The following snippet iterates over an object's properties, calculating their sum:

var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};

for each (var item in obj) {
  sum += item;
}

console.log(sum); // logs "26", which is 5+13+8

Specifications

Not part of a current ECMA-262 specification. Implemented in JavaScript 1.6 and deprecated.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support Not supported 1.5 (1.8) Not supported Not supported Not supported
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support Not supported Not supported 1.0 (1.0) Not supported Not supported Not supported

See also

  • for...in - a similar statement that iterates over the property names.
  • for...of - a similar statement that iterates over the property values but can only be used for iteratable types, so not for generic objects
  • for

Document Tags and Contributors

 Contributors to this page: JonTheFox
 Last updated by: JonTheFox,