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

G

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?
 
M

Martin Honnen

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

Gu

Gu wrote:



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";
 
G

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
 
L

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

Gu

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
 
T

Thomas 'PointedEars' Lahn

Gu said:
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
 
G

Grant Wagner

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

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top