473,323 Members | 1,574 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,323 software developers and data experts.

again.. objects (quite lame, i suppose..)

Gu
hi to all again!
well, if i have the simplest object:

a=new Object()

it seems i can't assign properties to it..

b.c="something"

as when i say:

writeln(b.c)

it return an error.. any suggestion?
Jul 23 '05 #1
7 1154


Gu wrote:

well, if i have the simplest object:

a=new Object()

it seems i can't assign properties to it..

b.c="something"


Above you create a variable named a now you try to set a property on b
that can't work.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Gu
On Sat, 14 May 2005 17:12:48 +0200, Martin Honnen <ma*******@yahoo.de>
wrote:


Gu wrote:

well, if i have the simplest object:

a=new Object()

it seems i can't assign properties to it..

b.c="something"


Above you create a variable named a now you try to set a property on b
that can't work.


just a mistyping, of course the code is:

a=new Object();
a.b="something";

Jul 23 '05 #3
Gu
anyway, i partially solved the problem (with the previous post i din't
mean that the problem was the mistyping but.. oh, well, nevermind).
where i'm aiming is a way to "dynamically" act on objects.
say i have the object kitchen created upon the object room. i take an
imput from the user asking him which room he would like to know the
description of:

function room(){
this.description="blabla";
}

kitchen=new room();
kitchen.description="blablabla2";

(don't mind eventual typo errors, i'm not copy pasting it and the
problem is a "conceptual" one). now, the inprut from the user (or from
a function, this is not the point) is kitchen. i could do this:

document.writeln(eval(userInput+".description"));
[where userInput is "kitchen"]
this actually work so far. but:
1 is there a way to avoid using eval?
2 what if the input is, say, "bathroom" which is not set yet? how can
i check if given object exists?

thanks in advance
Jul 23 '05 #4
Lee
Gu said:

anyway, i partially solved the problem (with the previous post i din't
mean that the problem was the mistyping but.. oh, well, nevermind).
where i'm aiming is a way to "dynamically" act on objects.
say i have the object kitchen created upon the object room. i take an
imput from the user asking him which room he would like to know the
description of:

function room(){
this.description="blabla";
}

kitchen=new room();
kitchen.description="blablabla2";

(don't mind eventual typo errors, i'm not copy pasting it and the
problem is a "conceptual" one). now, the inprut from the user (or from
a function, this is not the point) is kitchen. i could do this:

document.writeln(eval(userInput+".description") );
[where userInput is "kitchen"]
this actually work so far. but:
1 is there a way to avoid using eval?
2 what if the input is, say, "bathroom" which is not set yet? how can
i check if given object exists?


Any time you're going to post code and say that it doesn't work,
copy and paste it from code that you've tested, or you just waste
everybody's time.

Here are some examples of how you can set and get attributes of
a custom object:

<html>
<body>
<script>
function Room(name,width,length) {
this.name=name;
this.width=width;
this.length=length;
}
var alpha=new Room("Dining",12,18);
alpha.color="burgundy";
alpha["flooring"]="hardwood";

alert(alpha.name);
var dimension="width";
alert(alpha[dimension]);
if(alpha.color) {
alert(alpha.color);
}
var str="";
for (attr in alpha) {
str += attr + ": \"" + alpha[attr] + "\"\n";
}
alert(str);
</script>
</body>
</html>

Jul 23 '05 #5
Gu
On 14 May 2005 10:04:04 -0700, Lee <RE**************@cox.net> wrote:
Gu said:

anyway, i partially solved the problem (with the previous post i din't
mean that the problem was the mistyping but.. oh, well, nevermind).
where i'm aiming is a way to "dynamically" act on objects.
say i have the object kitchen created upon the object room. i take an
imput from the user asking him which room he would like to know the
description of:

function room(){
this.description="blabla";
}

kitchen=new room();
kitchen.description="blablabla2";

(don't mind eventual typo errors, i'm not copy pasting it and the
problem is a "conceptual" one). now, the inprut from the user (or from
a function, this is not the point) is kitchen. i could do this:

document.writeln(eval(userInput+".description")) ;
[where userInput is "kitchen"]
this actually work so far. but:
1 is there a way to avoid using eval?
2 what if the input is, say, "bathroom" which is not set yet? how can
i check if given object exists?


Any time you're going to post code and say that it doesn't work,
copy and paste it from code that you've tested, or you just waste
everybody's time.

Here are some examples of how you can set and get attributes of
a custom object:

<html>
<body>
<script>
function Room(name,width,length) {
this.name=name;
this.width=width;
this.length=length;
}
var alpha=new Room("Dining",12,18);
alpha.color="burgundy";
alpha["flooring"]="hardwood";

alert(alpha.name);
var dimension="width";
alert(alpha[dimension]);
if(alpha.color) {
alert(alpha.color);
}
var str="";
for (attr in alpha) {
str += attr + ": \"" + alpha[attr] + "\"\n";
}
alert(str);
</script>
</body>
</html>


hi lee, thanks for your reply. your codes are useful, but the suppose
i know i' m working on "alpha", while i really don't know if the user
tells javascript that the room he wants info about is "alpha" and not
"beta"
thnaks
Jul 23 '05 #6
Gu wrote:
function room(){
this.description="blabla";
}

kitchen=new room();
kitchen.description="blablabla2";
I'd rather use

function Room(sDescr)
{
this.description = sDescr || "blabla";
}

var kitchen = new Room("blablabla2");
(don't mind eventual typo errors, i'm not copy pasting it and the
problem is a "conceptual" one).
Even wrong or misunderstood concepts can hardly be recognized by outsiders
when you post the wrong code.

Especially, you posted the code that does not work but you did not explain
where and how exactly it does not work. The "typos" aside, your code *up
to here* appears to be syntactically and conceptually OK.
now, the inprut from the user (or from
a function, this is not the point) is kitchen. i could do this:

document.writeln(eval(userInput+".description"));
Eeek.
[where userInput is "kitchen"]
this actually work so far. but:
1 is there a way to avoid using eval?
Yes, and it is explained in the FAQ as well as in countless postings.
2 what if the input is, say, "bathroom" which is not set yet? how can
i check if given object exists?


Obviously you did not use any (group) search engine prior.
PointedEars
--
The German psychs, the German authorities, the German secret service agents
are [...] fanatics, they are insane and known of persecuting innocent people
and Scientologists. -- "The only real Barbara Schwarz", dsw.scientology,
<16**************************@posting.google.com >
Jul 23 '05 #7
"Gu" <pi********@gmail.com> wrote in message
news:b9********************************@4ax.com...
On Sat, 14 May 2005 17:12:48 +0200, Martin Honnen <ma*******@yahoo.de>
wrote:


Gu wrote:

well, if i have the simplest object:

a=new Object()

it seems i can't assign properties to it..

b.c="something"


Above you create a variable named a now you try to set a property on b
that can't work.


just a mistyping, of course the code is:

a=new Object();
a.b="something";


var a = new Object(); // or {};
a.b = "something";
document.writeln(a.b);

Tested and working in IE 6.0.2900, Firefox 1.0.4, Netscape 4.8, Opera
8.00, Mozilla 1.7.8. Quite frankly, it should work in any Web browser
that supports a default HTMLDocument object called document and
ECMA-262.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #8

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

Similar topics

34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
9
by: Relaxin | last post by:
Is it possible to CREATE (not use) COM objects with C#? If so, can you point in the right direction? Thanks
9
by: Christian Blackburn | last post by:
Hi Gang, I've had this happen with a couple of controls now, but my patience has worn thin. Can somebody tell me why I can read/write to most objects on my form from my module, but not when...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
28
by: Frederick Gotham | last post by:
When I was a beginner in C++, I struggled with the idea of references. Having learned how to use pointers first, I was hesitant to accept that references just "do their job" and that's it. Just...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
28
by: walterbyrd | last post by:
Python seems to have a log of ways to do collections of arbitrary objects: lists, tuples, dictionaries. But what if I want a collection of non-arbitrary objects? A list of records, or something...
2
by: =?Utf-8?B?anVhbg==?= | last post by:
Hello: I have tried for weeks to use LAME in Windows. Can somebody tell me how to do it? I can't add a reference to the project (Visual Basic 2005). Thanks.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.