/    Sign up×
Community /Pin to ProfileBookmark

window["somevarible"+i] question

I have only learned recently that you can use
[color=green]window[“somevarible”+i][/color] instead of
[color=red]eval(“somevarible”+i)[/color]

So that works with global varibles — outside of functions.
But how do you “get” a local varible — inside a function?

[code=html]
<script type=”text/javascript”>
onload=function(){
var somevarible1=”hi”
alert(window[“somevarible”+1])
/* Alerts undefined */
}
</script>
[/code]

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@acorbelliApr 01.2005 — I'm afraid I don't quite understand what you're trying to accomplish with the script...

It looks to me like you're trying to "alert()" the value at window[hi1].

If you want to print out in the alert the message stored in the variable called "hi1" then I would use eval......
[CODE]alert(eval("somevarible"+1));[/CODE]
which will print out whatever is stored in the variable named "hi1".

Please forgive me if I misinterperete the code though....
Copy linkTweet thisAlerts:
@UltimaterauthorApr 01.2005 — Yes, eval will work, but it should be avoided whenever possible.

My question is how would you replace the usage of eval in this Situation.

If the varible [color=green]somevarible1[/color] wasn't a local varible but

rather a gobal varible, we would be able to access it like so:
[code=html]
<script type="text/javascript">
var somevarible1="hi"//gobal varible
onload=function(){
alert(window["somevarible"+1])
}
</script>
[/code]
Copy linkTweet thisAlerts:
@acorbelliApr 01.2005 — I think I get you now, you want to know how you can access the value in somevariable1 within a function and possibly as a loop (somevariable1, somevariable2, somevariable3...etc...)?

What you are doing there should work just fine, it's the same inside functions as outside functions....if I am understanding what you are saying....
Copy linkTweet thisAlerts:
@UltimaterauthorApr 01.2005 — No, it's not the same inside and outside of functions, otherwise I wouldn't have

started this thread.

If you try executing both of the codes that I provided, you will recieve different alerts.

The first code will alert "undefined" and the second code will alert "hi".

If you're recieving "hi" in both examples, then it's a browser issue.

(I'm using IE6.0)

And yes, you understand what I am trying to do, now.
Copy linkTweet thisAlerts:
@acorbelliApr 01.2005 — I just realised what I had here didnt work fo the context you are looking for. Working on a solution...

A quick aside, do you need this for a project? (Like is one of the requirements to use window["variable"])?

If this is not a requirement I would suggest simply using an array and a loop to go through them, may be easier in the long run...though it's all in context.

Still looking for a solution though, sorry I haven't been of much help...
Copy linkTweet thisAlerts:
@UltimaterauthorApr 01.2005 — Hmm... Maybe it would be easier if we actually name the function.
[code=html]
<script type="text/javascript">
onload=ONLD
function ONLD(){
var somevarible1="hi"
alert(window["somevarible"+1])//undefined
}
</script>
[/code]
Copy linkTweet thisAlerts:
@Jeff_MottApr 01.2005 — Just make your local variable an array/object and index it appropriately.function f()
{
var somevariable = {};

<i> </i>somevariable[1] = "hi";
<i> </i>alert(somevariable[1]);
}
Copy linkTweet thisAlerts:
@UltimaterauthorApr 01.2005 — That's not what I'm trying to do, sorry.

Arrays is a workaround method. I'm trying to learn how to refer to a local

variable when you only have a string. The ideal way is by eval, but I want to

learn another method like [color=red]window["variable"][/color] to access a

variable w/o having to call eval. My goal is find all the methods that make eval

useless. Or even better yet, design a function that evaluates better than eval.
Copy linkTweet thisAlerts:
@JonaApr 01.2005 — [font=trebuchet ms]You can refer to a function by its name via [i]window["functionName"[/i], but there is no way to refer to a variable value in a function. E.g., [i]window["functionName"]["variableName"][/i]. I can't see why you'd want to do this, though, since you could just as easily use an array or some other method to avoid eval. Basically, there's no way to do what you want, but if you ever need to do what you want, there should be another way to do it. I hope I made that clear - it's late and I probably confused you. (I apologize if that's the case!)[/font]
Copy linkTweet thisAlerts:
@UltimaterauthorApr 01.2005 — Well, it makes sense to me, if I can't figure it out, no-one can! ?
Copy linkTweet thisAlerts:
@FangApr 01.2005 — I believe a local variable is only created when the function is run. The variable is removed/delete when the function ends.
Copy linkTweet thisAlerts:
@Jeff_MottApr 01.2005 — The ideal way is by eval, but I want to

learn another method like window["variable"] to access a

variable w/o having to call eval.[/quote]
As Jone said, there is no way to do this simply because there is no reason to [i]need[/i] to do this. And even if you could, there isn't a good reason why you [i]should[/i] do this either. If two or more variables are related then they should be grouped into their own data structure, thus an array or object.
×

Success!

Help @Ultimater spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...