این ترجمه ناقص است. لطفاً در ترجمه این مقاله از انگلیسی کمک کنید.

The for each...in statement is deprecated as the part of ECMA-357 (E4X) standard. E4X support has been removed, but for each...in will not be disabled and removed because of backward compatibility considerations. Consider using for...of instead. (Please refer to bug 791343.)

استیتمنت each...in یک متغیر مشخص شده را در سرتاسر مقادیر خاصیت‌های شی تکرار می‌کند.  برای هر خاصیت متمایز، یک استیتمنت مشخص شده اجرا شده است.

سینتکس

for each (variable in object) {
  statement
}
متغیر
Variable to iterate over property values, optionally declared with the var keyword. This variable is local to the function, not to the loop.
شی
Object for which the properties are iterated.
استیتمنت
A statement to execute for each property. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.

Description

Some built-in properties are not iterated over. These include all built-in methods of objects, e.g. String's indexOf 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

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

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

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: majidlx
Last updated by: majidlx,