again.. objects (quite lame, i suppose..) | | |
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? | | | | re: again.. objects (quite lame, i suppose..)
Gu wrote:
[color=blue]
> well, if i have the simplest object:
>
> a=new Object()
>
> it seems i can't assign properties to it..
>
> b.c="something"[/color]
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/ | | | | re: again.. objects (quite lame, i suppose..)
On Sat, 14 May 2005 17:12:48 +0200, Martin Honnen <mahotrash@yahoo.de>
wrote:
[color=blue]
>
>
>Gu wrote:
>
>[color=green]
>> well, if i have the simplest object:
>>
>> a=new Object()
>>
>> it seems i can't assign properties to it..
>>
>> b.c="something"[/color]
>
>Above you create a variable named a now you try to set a property on b
>that can't work.[/color]
just a mistyping, of course the code is:
a=new Object();
a.b="something"; | | | | re: again.. objects (quite lame, i suppose..)
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 | | | | re: again.. objects (quite lame, i suppose..)
Gu said:[color=blue]
>
>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?[/color]
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> | | | | re: again.. objects (quite lame, i suppose..)
On 14 May 2005 10:04:04 -0700, Lee <REM0VElbspamtrap@cox.net> wrote:
[color=blue]
>Gu said:[color=green]
>>
>>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?[/color]
>
>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>[/color]
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 | | | | re: again.. objects (quite lame, i suppose..)
Gu wrote:
[color=blue]
> function room(){
> this.description="blabla";
> }
>
> kitchen=new room();
> kitchen.description="blablabla2";[/color]
I'd rather use
function Room(sDescr)
{
this.description = sDescr || "blabla";
}
var kitchen = new Room("blablabla2");
[color=blue]
> (don't mind eventual typo errors, i'm not copy pasting it and the
> problem is a "conceptual" one).[/color]
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.
[color=blue]
> 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"));[/color]
Eeek.
[color=blue]
> [where userInput is "kitchen"]
> this actually work so far. but:
> 1 is there a way to avoid using eval?[/color]
Yes, and it is explained in the FAQ as well as in countless postings.
[color=blue]
> 2 what if the input is, say, "bathroom" which is not set yet? how can
> i check if given object exists?[/color]
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,
<16d1deb5.0402260945.4d4316af@posting.google.com > | | | | re: again.. objects (quite lame, i suppose..)
"Gu" <pistacchio@gmail.com> wrote in message
news:b98c81pfdj1jiqtl30jm8ssn0h6033mu4m@4ax.com...[color=blue]
> On Sat, 14 May 2005 17:12:48 +0200, Martin Honnen <mahotrash@yahoo.de>
> wrote:
>[color=green]
>>
>>
>>Gu wrote:
>>
>>[color=darkred]
>>> well, if i have the simplest object:
>>>
>>> a=new Object()
>>>
>>> it seems i can't assign properties to it..
>>>
>>> b.c="something"[/color]
>>
>>Above you create a variable named a now you try to set a property on b
>>that can't work.[/color]
>
> just a mistyping, of course the code is:
>
> a=new Object();
> a.b="something";[/color]
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 <gwagner@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|