faqts : Computers : Programming : Languages : JavaScript : Language Core : Objects

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

27 of 32 people (84%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How do I expose the properties of an object?

Nov 14th, 2002 03:36
Mark Filipak, Martin Honnen, Philippe Rousseliere,


Properties of an object are exposed. There are no private properties in 
JavaScript (1.0 - 1.5). You script
  objectReference.propertyName
or
  objectReference['propertyName']
--- begin addition to Martin's answer ---
By 'expose', perhaps you mean 'enumerate' (i.e., list). You can
enumerate the names of the properties of any object with a for-loop as
follows:
  for (myPropName in myObject) alert(myPropName);
where myObject is a previously assigned object. For example,
  var s = '';
  for (p in document.body) {
    s += 'document.body.'+p+' = '+eval('document.body.'+p)+'<br>';
  }
  document.write(s);
will list the names and values of all enumerable properties of
document.body. Using this technique you can write your own DOM browser
by adding links to the output list. Have fun. It is a worthwhile project
that took me only one month. 8^)
--- end addition to Martin's answer --- Mark Filipak