473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Basic OO question.`

How do I iterate through an object to pull out the object's property
and then the property's value. Take this code for instance:

var book = new Object();
book.title = "javascript: The Definitive Guide";

function displayPropertyNames(obj) {
var names = "";
for(var name in obj) names += name + ": " + obj.name + "<br>";
document.write(names);
}
displayPropertyNames(book);

This produces:

title: Undefined

What I want is:

title: javascript: The Definitive Guide

What am I doing wrong.

Thanks in advance.

:)
SA

Jul 23 '05 #1
3 950
Bucco wrote:

[Square Bracket Notation]
for(var name in obj) names += name + ": " + obj.name + "<br>";


The square bracket notation lets you use a string variable as
identifier: obj[name] is what you're looking for!

<URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>
HTH,
Yep.
Jul 23 '05 #2
ASM
Bucco wrote:
How do I iterate through an object to pull out the object's property
and then the property's value. Take this code for instance:

var book = new Object();
book.title = "javascript: The Definitive Guide";

function displayPropertyNames(obj) {
var names = "";
for(var name in obj) names += name + ": " + obj.name + "<br>";
for(var name in obj) names += name + ": " + obj[name] + "<br>";
document.write(names);
}
displayPropertyNames(book);


<style type="text/css">
#obj p { text-align: right; background: #ff6;margin:1px auto;
width:520px; clear:left}
#obj span { float:right;width: 400px;
color:blue;text-align:left;padding-left:8px }
</style>
<script type="text/javascript">
var book = new Object();
book.title = "javascript: The Definitive Guide";
book.src = "folder/file.jpg"
book.onclick = "alert('it works');";

function displayPropertyNames(obj) {
var names = "";
for(var name in obj)
names += '<p><span>' + obj[name] + '<\/span>'+ name + ' = ' ;
document.write('<div id="obj">'+names+'<\/div>');
}
displayPropertyNames(book);
</script>
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #3
Lee
Bucco said:

How do I iterate through an object to pull out the object's property
and then the property's value. Take this code for instance:

var book = new Object();
book.title = "javascript: The Definitive Guide";

function displayPropertyNames(obj) {
var names = "";
for(var name in obj) names += name + ": " + obj.name + "<br>";
document.write(names);
}
displayPropertyNames(book);

This produces:

title: Undefined

What I want is:

title: javascript: The Definitive Guide

What am I doing wrong.


The "dot" notation (obj.name) requires that anything that appears
after a dot be a literal name, not a variable. With variables,
you use the bracket notation:

+ obj[name] +

Jul 23 '05 #4

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

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
1
by: frankhanretty | last post by:
Do I have to install Visual basic on the remote terminals as I did on the server? I have an visual basic 5 application running fine on my client's server and he is now networked. He wants to run the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...
0
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
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
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,...

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.