Basic HTML version of Foils prepared 23 June 1997

Foil 30 The for-loop Revisited

From JavaScript Tutorial for Certificate Course UC Web applications Certificate -- Summer 97. by Geoffrey C. Fox, Tom Scavo


1 One can iterate over all properties of an object:
2 for ( variable in object ) { É }
3 function dump_props(obj, obj_name) {
4 var result = "";
5 for ( var i in obj ) {
  • result +=
  • obj_name + "." + i + " = " + obj[i] + "<BR>";
6 }
7 return result + "<HR>";
8 }
9 Here i is an index and obj[i] is the actual property
10 Note user supplies obj and obj_name:
11 function car(make, model) { // An Example
12 this.make = make; this.model = model;
13 }
14 mycar = new car("Ford", "Explorer");
15 document.writeln(dump_props(mycar, "mycar"));

in Table To:


Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed Apr 1 1998