473,513 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

listing object properties and/or method

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 ...
--
une bévue
Sep 30 '06 #1
6 1827
VK

Une bévue wrote:
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];
}

Sep 30 '06 #2
VK <sc**********@yahoo.comwrote:
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];
}
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 ?
--
une bévue
Sep 30 '06 #3
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...

Sep 30 '06 #4
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);

Sep 30 '06 #5
VK

VK wrote:
// 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 :-)

Sep 30 '06 #6
VK <sc**********@yahoo.comwrote:
sorry, typed of my head... Of course this has to be changed:
ok, fine, thanks !
--
une bévue
Sep 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1500
by: Bengt Richter | last post by:
Still looking for a unifying concept ;-) It struck me that @deco1 @deco2 def foo():pass uses '@' to register at-next-name-binding processing in a way similar to atexit's registering of...
54
4492
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
16
25393
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
4
1535
by: jbesr1230 | last post by:
Hello, How would I print out a listing of the queries that are in a database? I would like to have a spreadsheet style listing with the columns of: Query Name, Description, Modified Date, Created...
2
991
by: Stu | last post by:
Hi, I have a class with quite a large number of properties that stores booking information.. I need to create an audit trail for these bookings every time something is changed. I thought about...
11
3797
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
1843
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
12
5506
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
4
1044
by: John | last post by:
Hi Is there an easy or a built-in way to collect all properties of an object into a string variable for debugging purpose? I am looking for something similar to this; "Property1:...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.