HELP! * GREY=local LOCAL HTML version of Foils prepared 26 February 1996

Foil 20 The for ... in JavaScript syntax

From CPS616 Lectures on JavaScript CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 96. by Geoffrey C. Fox * See also color IMAGE

This allows one to iterate over all the properties of an object
for ( variable in object ) { // Generic Form
Bunch of statements }
function dump_props(obj, obj_name) {
var result = "";
for( var i in obj) {
result += obj_name + "." + i + " = " + obj[i] + "<BR>"; }
result += "<HR>";
return result;
}
Here i runs over names of Properties and obj[i] is 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 Tue Feb 18 1997