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>