Advertisement

05.26.2008 at 11:40PM PDT, ID: 23434009
[x]
Attachment Details

Create variables at runtime named by passed arguments into a function

Asked by smirking in Scripting Languages, JavaScript

Tags: JavaScript

I need to create variables at runtime based on variables passed into a function. See the sample code snippet below for an example of what I need to do.

In the example below, I want to create a variable named "tootsie" that's named after the value of myVar variable that's passed into the function. How do I code sampleMethod so that my script will output the alert message "My name is Tootsie"?


Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
function sampleMethod( myVar ) {
   var myVar = "My name is Tootsie;
   alert(tootsie);
}
 
<script>
sampleMethod( 'tootsie' );
</script>
[+][-]05.26.2008 at 11:45PM PDT, ID: 21649817

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.26.2008 at 11:57PM PDT, ID: 21649860

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 12:02AM PDT, ID: 21649880

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 12:16AM PDT, ID: 21649940

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 12:30AM PDT, ID: 21650001

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 12:32AM PDT, ID: 21650015

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Scripting Languages, JavaScript
Tags: JavaScript
Sign Up Now!
Solution Provided By: Roonaan
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628