Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / September 2006



Tip: Looking for answers? Try searching our database.

listing object properties and/or method

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Une bévue - 30 Sep 2006 16:40 GMT
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 ...

Signature

une bévue

VK - 30 Sep 2006 18:07 GMT
> 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[i];
}
Une bévue - 30 Sep 2006 18:34 GMT
> for (var p in myObject) {
>  document.forms[0].elements['out'].value +=
[quoted text clipped - 18 lines]
>  document.forms[0].elements['out'].value+= props[i];
> }

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 ?
Signature

une bévue

VK - 30 Sep 2006 19:22 GMT
> 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...
VK - 30 Sep 2006 19:26 GMT
>  // 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);
VK - 30 Sep 2006 19:45 GMT
> >  // methods reported as objects by IE and as functions by others:
> >  desc+= ((typeof p == 'function') || (typeof p == 'object)) ?
[quoted text clipped - 5 lines]
>
> 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 :-)
Une bévue - 30 Sep 2006 19:56 GMT
> sorry, typed of my head... Of course this has to be changed:

ok, fine, thanks !
Signature

une bévue

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.