listing object properties and/or method

U

Une bévue

i'd like to know objects properties and/or methods.

saying i do have an object "o" being part of the dom (a div or an ul...)
how could i list (introspection) all the properties and methods attached
to this object ?

i know it is possible in javascript but don't remeber how to ...
 
V

VK

Une said:
saying i do have an object "o" being part of the dom (a div or an ul...)
how could i list (introspection) all the properties and methods attached
to this object ?

for (var p in myObject) {
document.forms[0].elements['out'].value +=
myObject[p] + ' = ' + p + '\n';
}

That's presuming you have a single form on your page with textarea
element named "out". Sure you can output anywhere you want, say to
innerHTML of some div.

It is often more convenient to view properties alphabetically:

var props = new Array();

for (var p in myObject) {
props.push(myObject[p] + ' = ' + p + '\n');
}

props.sort();

for (var i=0; i<props.length; i++) {
document.forms[0].elements['out'].value+= props;
}
 
U

Une bévue

VK said:
for (var p in myObject) {
document.forms[0].elements['out'].value +=
myObject[p] + ' = ' + p + '\n';
}

That's presuming you have a single form on your page with textarea
element named "out". Sure you can output anywhere you want, say to
innerHTML of some div.

It is often more convenient to view properties alphabetically:

var props = new Array();

for (var p in myObject) {
props.push(myObject[p] + ' = ' + p + '\n');
}

props.sort();

for (var i=0; i<props.length; i++) {
document.forms[0].elements['out'].value+= props;
}


fine thanks a lot !

but, when u do :

var p in myObject

p is all the properties, does that include methods ?

or, as in ruby (somehow) a method could be a property of the object ?
 
V

VK

p is all the properties, does that include methods ?
or, as in ruby (somehow) a method could be a property of the object ?

For the answer you could try this on say window object ;-)

Yes, "property" term here includes fields, properties (aka compound
properties with getter/setter) and methods. So this term is not an
exact equivalent of similar term used in some programming languages.
Think of it as "anything enumerable this object has".

You can write a full-scaled object walker with additional sortouts of
any complexity.

var props = new Array();
var desc = '';

for (var p in myObject) {
desc = myObject[p] + ' = ' + p + '\n';

desc+= myObject.hasOwnProperty(p) ?
'own ' : 'inherited ';

// methods reported as objects by IE and as functions by others:
desc+= ((typeof p == 'function') || (typeof p == 'object)) ?
'method\n\n' : 'property\n\n';

props.push(myObject[p] + ' = ' + p + '\n');

}

props.sort();

and further...
 
V

VK

// methods reported as objects by IE and as functions by others:
desc+= ((typeof p == 'function') || (typeof p == 'object)) ?
'method\n\n' : 'property\n\n';

props.push(myObject[p] + ' = ' + p + '\n');

sorry, typed of my head... Of course this has to be changed:

props.push(desc);
 
V

VK

VK said:
// methods reported as objects by IE and as functions by others:
desc+= ((typeof p == 'function') || (typeof p == 'object)) ?
'method\n\n' : 'property\n\n';

props.push(myObject[p] + ' = ' + p + '\n');

sorry, typed of my head... Of course this has to be changed:

props.push(desc);

And thinking it over again :) that would kill the branching, so in
case if some property is in turn an object with its own properties,
the walker will report it as "method" which is plain wrong. Because of
IE's ambigousity (both method and object reported as "object") you have
to think for a workaround; and then make your walker recursive for
branches, but it's getting on the industry level already, not a
quick'n'durty helper :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top