Baseline Widely available
è¿ä»£ç©ä»¶çå¯åè屬æ§ãå°æ¯åç¸ç°å±¬æ§ï¼å·è¡é³è¿°å¼ã
Statement Implemented in: JavaScript 1.0 ECMA Version: ECMA-262 èªæ³for (è®æ¸ in ç©ä»¶) {... }忏
è®æ¸
A different property name is assigned to variable on each iteration.
ç©ä»¶
Object whose enumerable properties are iterated.
for...in
è¿´ååªè¿ä»£å¯åè屬æ§ãç±å
§å»ºå»ºæ§å¼(å¦ï¼ArrayãObject) 製é çç©ä»¶ï¼å¾ Object.prototype
å String.prototype
ç¹¼æ¿äºä¸å¯åè屬æ§ï¼å¦ï¼ String
çindexOf
æ¹æ³ï¼æ Object
ç toString
æ¹æ³ã è¿´åå°è¿ä»£å
¨é¨å¯åè屬æ§ï¼å
æ¬äºç©ä»¶èªèº«çåç©ä»¶ç¹¼æ¿èªå®ç建æ§å¼ä¹ååçå¯åè屬æ§ã(ååéä¸è¼æ¥è¿ç©ä»¶ç屬æ§è¦èååç屬æ§)
A for...in
loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting). If a property is modified in one iteration and then visited at a later time, its value in the loop is its value at that later time. A property that is deleted before it has been visited will not be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify or remove properties from the object during iteration, other than the property currently being visited. There is no guarantee whether or not an added property will be visited, whether a modified property (other than the current one) will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.
åè¨»ï¼ If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used). Alternatively, if you know there won't be any outside code interference, you can extend built-in prototypes with a check method.
åè¨»ï¼ for..in
ä¸æè©²ç¨ä¾è¿ä»£ä¸åç´¢å¼é åºå¾éè¦çé£åã é£åç´¢å¼åªæ¯ä»¥æ´æ¸å½åçå¯åè屬æ§ï¼å
¶ä»æ¹é¢çåæ¼ä¸è¬ç©ä»¶å±¬æ§ã ç¡æ³æä¿ for...in
以ç¹å®é åºå³åç´¢å¼ï¼ä¸¦ä¸å®å°å³åå
¨é¨å¯åè屬æ§ï¼å
æ¬éæ´æ¸åçï¼ä»¥åç¹¼æ¿èä¾çå¯åè屬æ§ãå çºè¿ä»£çé åºä¾è³´æ¼ JavaScript 弿ç實ä½ï¼å¨ä¸å弿ä¸ï¼è¿ä»£ä¸åé£åå¯è½ä¸æ¯ä»¥ä¸åä¸è´çé åºååé£åå
ç´ ãå æ¤ï¼ç¶ä½ è¿ä»£é£åï¼ä¸è©²é£åçååé åºå¾éè¦æï¼æå¥½æ¯ä½¿ç¨ä»¥æ¸å¼ç´¢å¼ç for è¿´å (æ Array.forEach æéæ¨æº for...of
è¿´å)ã
The following function takes as its arguments an object and the object's name. It then iterates over all the object's enumerable properties and returns a string of the property names and their values.
var o = { a: 1, b: 2, c: 3 };
function show_props(obj, objName) {
var result = "";
for (var prop in obj) {
result += objName + "." + prop + " = " + obj[prop] + "\n";
}
return result;
}
alert(
show_props(o, "o"),
); /* alerts (in different lines): o.a = 1 o.b = 2 o.c = 3 */
The following function illustrates the use of hasOwnProperty: the inherited properties are not displayed.
var triangle = { a: 1, b: 2, c: 3 };
function ColoredTriangle() {
this.color = "red";
}
ColoredTriangle.prototype = triangle;
function show_own_props(obj, objName) {
var result = "";
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
result += objName + "." + prop + " = " + obj[prop] + "\n";
}
}
return result;
}
o = new ColoredTriangle();
alert(show_own_props(o, "o")); /* alerts: o.color = red */
è¦ç¯ ç覽å¨ç¸å®¹æ§ See also
for...of
- a similar statement that iterates over the property valuesfor each...in
- a similar statement, but iterates over the values of object's properties, rather than the property names themselves (New in JavaScript 1.6 but deprecated)for...in
syntax)getOwnPropertyNames
hasOwnProperty
Array.prototype.forEach
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4