The Object of My Desire
January 21, 2007 9:46 AM Subscribe
JavaScriptFilter. I'm learning JavaScript and I'm trying to find a way to call object X.
I have several objects:
...and I have a function:
Now, instead of calling cellObj1, I want to call cellObjX (X being cellnumber. If cellnumber=4, I want cellObj4 called). I've tried:
This is all new to me. I'm completely stumped.
I have several objects:
cellObj0, cellObj1, cellObj2...
...and I have a function:
function cellLunch(cellnumber)
{
alert(cellObj1.lightlinks)
}
Now, instead of calling cellObj1, I want to call cellObjX (X being cellnumber. If cellnumber=4, I want cellObj4 called). I've tried:
alertObj+cellnumber
, and, alertObjcellnumber
but it's not doing what I want it to do.This is all new to me. I'm completely stumped.
Response by poster: Though for some reason, that doesn't work either. It's not throwing up any error messages or anything, it's just not doing anything. Does the array have to be defined in any particular way?
posted by popcassady at 10:16 AM on January 21, 2007
posted by popcassady at 10:16 AM on January 21, 2007
If, for some reason, you can't use an array, there is another way:
posted by Deathalicious at 10:33 AM on January 21, 2007
function cellLunch(cellnumber)
{
thisObj=eval('cellObj'+cellnumber);
alert(thisObj.lightlinks)
}
posted by Deathalicious at 10:33 AM on January 21, 2007
You can use the eval method that Deathalicious posted, but in general it is better to use arrays to store data and not just a bunch of sequentially numbered variables.
posted by Rhomboid at 10:51 AM on January 21, 2007
posted by Rhomboid at 10:51 AM on January 21, 2007
Response by poster: Thanks all. I'm not too sharp today, I've figured why the arrays weren't working.
Deathalicious... thanks for the eval pointer.
posted by popcassady at 11:07 AM on January 21, 2007
Deathalicious... thanks for the eval pointer.
posted by popcassady at 11:07 AM on January 21, 2007
Just for the record, you don't call objects -- you call methods. I only mention this because it definitely marks you as a beginner. (Nothing wrong with being a beginner, but you will probably want to stop looking like one at some point, and you might as well start now.)
eval is generally considered a tactic of last resort, just by the way, and may have negative performance implications, so the array of objects is definitely the way to go.
posted by kindall at 2:07 PM on January 21, 2007
eval is generally considered a tactic of last resort, just by the way, and may have negative performance implications, so the array of objects is definitely the way to go.
posted by kindall at 2:07 PM on January 21, 2007
the "proper" way to do it is an array, but the "easy" way to do it is with eval. In most programming languages, the eval-ability isn't there, but in javascript it is.
posted by delmoi at 2:59 PM on January 21, 2007
posted by delmoi at 2:59 PM on January 21, 2007
The Golden Rule of Eval: "If you need to use eval() in your code, then 99.999% of the time you're doing something wrong."
Torgo's Reply to the Golden Rule of Eval: "Not enough nines."
Here is the proper to reference those objects (if for some strange reason you don't want to use an array:
Warning: This is not doable in a non-global context, so it wouldn't work in a function, so the better way is to use an array.
posted by furtive at 3:26 PM on January 21, 2007
Torgo's Reply to the Golden Rule of Eval: "Not enough nines."
Here is the proper to reference those objects (if for some strange reason you don't want to use an array:
this["cellObj" + x]
Warning: This is not doable in a non-global context, so it wouldn't work in a function, so the better way is to use an array.
posted by furtive at 3:26 PM on January 21, 2007
The Golden Rule of Eval: "If you need to use eval() in your code, then 99.999% of the time you're doing something wrong."
Or else you're writing Lisp. ;)
posted by kindall at 7:54 PM on January 21, 2007
Or else you're writing Lisp. ;)
posted by kindall at 7:54 PM on January 21, 2007
Or else you're writing Lisp. ;)
Even if you’re writing Lisp, ask yourself can you avoid it. At least in Emacs Lisp, where if you're calling it it means its argument probably isn't byte-compiled, and is probably slow.
posted by Aidan Kehoe at 2:35 AM on January 22, 2007
Even if you’re writing Lisp, ask yourself can you avoid it. At least in Emacs Lisp, where if you're calling it it means its argument probably isn't byte-compiled, and is probably slow.
posted by Aidan Kehoe at 2:35 AM on January 22, 2007
This thread is closed to new comments.
var cellObjects = [cellObj0, cellObj1, cellObj2, ...];
Then, to access them:
function cellLunch(cellnumber)
{
alert(cellObjects[cellnumber].lightlinks);
}
posted by Khalad at 9:51 AM on January 21, 2007