for...in statement

 

The for...in statement iterates a specified variable over all the enumerable properties of an object. For each distinct property, JavaScript executes the specified statements. Afor...in statement looks as follows:

for (variable in object) {
  statements
}

  

Example

The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.

function dump_props(obj, obj_name) {
  var result = "";
  for (var i in obj) {
    result += obj_name + "." + i + " = " + obj[i] + "<br>";
  }
  result += "<hr>";
  return result;
}
 

  

For an object car with properties make and modelresult would be:

car.make = Ford
car.model = Mustang

  

posted @ 2015-06-25 23:50  hephec  阅读(175)  评论(0编辑  收藏  举报