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


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



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