469,271 Members | 1,759 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

How to inspect *all* object properties?

kj

Is there any way to programmatically discover and inspect *all*
the properties of an object, even those that are not enumerable
through a for/in loop?

Thanks!

kj

--
NOTE: In my address everything before the period is backwards.
Jul 23 '05 #1
9 12181
kj wrote:
Is there any way to programmatically discover and inspect *all*
the properties of an object, even those that are not enumerable
through a for/in loop?


If you can get a list of all possible (or, more realistically, likely)
property names then you can test any object by working through that list
testing to see if the object implements the properties. Non-enumerable
properties will be exposed in that process so long as their names
correspond with an item in the list.

Richard.
Jul 23 '05 #2
I'm not sure about "all", but if you use the MS script editor, you can walk
thru all the DOM objects and collections, inspecting each object for ID,
NAME and PARENT properties.

http://www.mandala.com/javascript/debug_javascript.html

I've written up a page explaining how to use the MS Script Editor for
Javascript debugging in IE. It's a great debugging environment and has
allowed my group to do much more with Javascript in IE with JSP files
than we would have attempted without it. This is NOT THE MS SCRIPT
DEBUGGER but a great editor/debugger included with Office XP/2000.
Very robust, and works great with JSP (and ASP), unlike the usual MS
tools like Visual Studio.

Please pass this around, as I think JSP developers especially need
this tool if they are using IE for development with complex
Javascript.

Of course, if you are using Netscape or Mozilla, Venkman is a good
debugger as well.

Cheers,
Jeff Papineau
San Jose, CA
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:cc*******************@news.demon.co.uk...
kj wrote:
Is there any way to programmatically discover and inspect *all*
the properties of an object, even those that are not enumerable
through a for/in loop?


If you can get a list of all possible (or, more realistically, likely)
property names then you can test any object by working through that list
testing to see if the object implements the properties. Non-enumerable
properties will be exposed in that process so long as their names
correspond with an item in the list.

Richard.

Jul 23 '05 #3
You mean properties of a DOM node?
Perhaps you mean properties in general objects, like those in an object
like, say, plugins.

=========
alert(typeof navigator.plugins);
for(var i in navigator.plugins){alert(i);}
=========
that generates erros in IE, for it is eventually reached _one_property that
refuses to be read. And this though the first alert says "obejct" and thus
it should have been 101% fit to be scanned by a for-in loop.

Yet these and other objects generate errors when scanned via SCRIPT by a
for-in loop. Some properties refuse to be read.
I don't know why, but probably there is either a read-protection device or a
private or protected signature in their prototypse, and if so there is
pretty little you can do to access them.

I hope I understood your question
ciao
Alberto
http://www.unitedscripters.com/
"kj" <so***@987jk.com> ha scritto nel messaggio
news:cc**********@reader2.panix.com...

Is there any way to programmatically discover and inspect *all*
the properties of an object, even those that are not enumerable
through a for/in loop?

Thanks!

kj

--
NOTE: In my address everything before the period is backwards.

Jul 23 '05 #4
Richard Cornford wrote:
kj wrote:
Is there any way to programmatically discover and inspect *all*
the properties of an object, even those that are not enumerable
through a for/in loop?


If you can get a list of all possible (or, more realistically, likely)
property names then you can test any object by working through that list
testing to see if the object implements the properties. Non-enumerable
properties will be exposed in that process so long as their names
correspond with an item in the list.

Richard.


<tongueincheek>

Or access the "typeof" every property name with 1 to 16 characters in the
set [a-zA-Z]. In other words:

[object].a, [object].b, ..., [object].aa, [object].ab, ...,
[object].aaaaaaaaaaaaaaaa, [object].aaaaaaaaaaaaaaab, ...,
[object].zzzzzzzzzzzzzzzz, then all uppercase [A-Z], then mixed [a-zA-Z].

If you set it up to run today, it would probably be done before you slept
the big sleep, but I wouldn't count on it.

I picked 16 character completely arbitrarily, and, as it turns out,
inappropriately, considering the W3C specified document.getElementsByTagName
is a method label with 20 characters in it, and even if the code generating
the list above finished in a reasonable period of time, it would have missed
getElementsByTagName.

You could probably make the code slightly more efficient by retrieving the
list of publically exposed properties and methods using for (... in ...),
then excluding those from the autogenerated list, but I doubt it would save
you much.

</tongueincheek>

As a mental exercise, I decided to waste my time trying this out. Here is a
general purpose function for finding all up to four lowercase letter
properties and methods on any object. Included is a test case for the
"document" object, and a code-snippet to show that the function does,
indeed, list all properties and methods up to four characters in the set
[a-z]:

function allProp(obj, previousProp, depth) {
if (!previousProp) {
previousProp = '';
}
var newProperty, properties = [];
for (var i = 97; i < 123; i++) {
newProperty = previousProp + String.fromCharCode(i);
if (previousProp.length < 3) {
properties = properties.concat(allProp(obj, newProperty));
}
if (typeof obj[newProperty] != 'undefined') {
properties.push(newProperty);
}
}
return properties;
}
var prop = allProp(document);

// code to test results
var s = [];
for (var i in prop) {
s.push(i + ' = ' + prop[i] + ' = ' + document[prop[i]]);
}
document.write(s.join('<br>'));

Note that while this runs quite snappily in IE, in Firefox 0.9.1, I got
about 6 alerts telling me script was causing the browser to run slowly. I
kept at it until it eventually finished, and discovered, much to my
amazement, that Firefox 0.9.1 supports "document.eval()".

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #5
Grant Wagner <gw*****@agricoreunited.com> writes:
<tongueincheek>

Or access the "typeof" every property name with 1 to 16 characters in the
set [a-zA-Z]. In other words: .... If you set it up to run today, it would probably be done before you slept
the big sleep, but I wouldn't count on it.
Nor would I. That's 52^17-2, or 148613013882162475899836956670,
(~148*10^27) different combinations.
If you test one thousand million combinatios every second, it'll take
4699612106676 (~4.7*10^12) years ... more than the projected lifetime
of the univers.

Ofcourse, any unicode letter can be used in a property name, so a-zA-Z
is a very drastic simplification as well.
As a mental exercise, I decided to waste my time trying this out. Here is a
general purpose function for finding all up to four lowercase letter
properties and methods on any object.


52^5-2 = 380204030. Only 380 million combinations. That's doable :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6
Grant Wagner wrote:
Richard Cornford wrote:
If you can get a list of all possible (or, more realistically,
likely) property names then you can ...
<snip> <tongueincheek>

Or access the "typeof" every property name with 1 to 16 characters in
the set [a-zA-Z]. In other words:

[object].a, [object].b, ..., [object].aa, [object].ab, ...,
[object].aaaaaaaaaaaaaaaa, [object].aaaaaaaaaaaaaaab, ...,
[object].zzzzzzzzzzzzzzzz, then all uppercase [A-Z], then mixed
[a-zA-Z].

If you set it up to run today, it would probably be done before you
slept the big sleep, but I wouldn't count on it. <snip> </tongueincheek>
Yes, trying all the possible property names (even just the ones allowed
by any javascript implementation, which probably limits the maximum
length of a property name in a way that the ECMA specification does not)
is not a practical proposition. I wonder whether assuming that property
names will be camel-case combinations of real words would make it
practical to a dictionary and try all the combination permutations of,
say, up to four words. Though I doubt that would help much and there is
no guarantee that non-enumerable property names are even in English so
it would be difficult to chose a dictionary for the task.

On the other hand there isn't an alternative to a list of some sort as
javascript offers no mechanism for exposing non-enumerable properties.
And to some extent the process works. Opera 6 does not list any
properties of its DOM elements with a - for(prop in obj) - loop, yet
trying a list of likely candidates did a fairly good job of exposing the
details of its object model. As listed at:-

<URL: http://www.litotes.demon.co.uk/dom_root.html >

And also exposed the interesting detail that Opera 6's DOM element
property names are mostly not particularly case sensitive (the
consequence of trying out likely case variants).

<snip> Note that while this runs quite snappily in IE, in Firefox 0.9.1, I
got about 6 alerts telling me script was causing the browser to run
slowly.
That is a running problem with my DOM scanning script. Different
browsers have different sensitivities to the duration of running scripts
and the more the process is split up with setTimeout calls the slower it
becomes (though not as slow as having to sit there and dismiss the
warning dialogs by hand).
I kept at it until it eventually finished, and discovered,
much to my amazement, that Firefox 0.9.1 supports "document.eval()".


Every object in the Gecko DOM has an - eval - method. It may have been
deprecated as a property of objects since JavaScript 1.3 (and never been
part of ECMAScript) but it hasn't actually been removed yet.

Richard.

Jul 23 '05 #7
Gee can anyone say "for (var i in obj){...}
See article by

Danny Goodman
(http://developer.netscape.com/viewso...odman_debug.ht
ml)
Code from Danny:
Listing 1

// Simple browser sniffing needed here
var isNav = (navigator.appName == "Netscape")
// Output list of properties for the object
function dumpProps(objName) {
var obj = eval(objName)
var msg = ""
var count = 0
var maxProps = 10
// Loop through properties of the object
for (var i in obj) {
if (i != "outerHTML" && i != "outerText" && i != "innerHTML" && i
!= "innerText" && i != "domain") {
msg += objName + "." + i + "=" + obj[i] + "\n"
if (count > maxProps) {
// Output a batch
if (isNav) {
java.lang.System.out.println(msg)
}
else {
alert(msg)
}
msg = ""
count = 0
continue
}
count++
}
}
// Output any leftovers
if (isNav) {
java.lang.System.out.println(msg)
} else {
alert(msg)
}
}
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #8
sidney johnson wrote:
Gee can anyone say "for (var i in obj){...}
See article by

<snip>

for(var prop in obj){ ....} - explicitly _does_not_ report
non-enumerable properties of objects (which is what the OP asked to do).

Richard.
Jul 23 '05 #9

oops sorry
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Robin Becker | last post: by
4 posts views Thread by Hans Georg Krauthaeuser | last post: by
2 posts views Thread by Fernando Perez | last post: by
5 posts views Thread by Tim Henderson | last post: by
5 posts views Thread by Brian Kitt | last post: by
reply views Thread by castironpi | last post: by
reply views Thread by Stef Mientki | last post: by
reply views Thread by Terry Reedy | last post: by
3 posts views Thread by Rafe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.