How to inspect *all* object properties?

K

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
 
R

Richard Cornford

kj said:
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.
 
J

Jman

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
 
A

Alberto

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/
 
G

Grant Wagner

Richard said:
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 + ' = ' + document[prop]);
}
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
L

Lasse Reichstein Nielsen

Grant Wagner said:
<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
 
R

Richard Cornford

<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.
</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).

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.
 
S

sidney johnson

Gee can anyone say "for (var i in obj){...}
See article by

Danny Goodman
(http://developer.netscape.com/viewsource/goodman_debug/goodman_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 + "\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)
}
}
 
R

Richard Cornford

sidney said:
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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top