Pedro Chagas
asked on
JavaScript function with variable value in the name
Hi E's, I want to know if it is possible do this thing, and how:
I have this 3 functions, cena1(), cena2() and cena3(), and I will call the function of the value of the variable number (var number = x), x can be 1,2 or 3.
So, when I call the function cena?(), I have to put the value of the variable in the name of the function in (?).
How I do that?
The best regards, JC
I have this 3 functions, cena1(), cena2() and cena3(), and I will call the function of the value of the variable number (var number = x), x can be 1,2 or 3.
So, when I call the function cena?(), I have to put the value of the variable in the name of the function in (?).
How I do that?
The best regards, JC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
personally, i would just use the index number as a javascript parameter, and define the different sections from within one function, instead of playing around with the global scope so much...
ASKER
Hi @big_monty, thanks for your attention.
Tell me one thing please, is not possible put the variable directly in the name of the function, something like cena'number'(){...}?
I believe your solution is the best when I start the project (beginning), my project are in the end, and I need just to know how I put the name of the variable in the name of the function.
Thank you.
~JC
Tell me one thing please, is not possible put the variable directly in the name of the function, something like cena'number'(){...}?
I believe your solution is the best when I start the project (beginning), my project are in the end, and I need just to know how I put the name of the variable in the name of the function.
Thank you.
~JC
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In that case, I have to follow your solution.
I will try later, and I give you feedback.
Thank you again.
~JC
I will try later, and I give you feedback.
Thank you again.
~JC
ASKER
Hi, I try your solution and seems work well, but I have a problem in my bellow code:
num1, 2 or 3, are inside function "funcao", seems not be a global function.
I make this question because this code example is the closest replica to my real code, and I have to find a solution.
Is there a solution, or should I open a new question for this new problem?
~JC
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script>
var numero = 1;
var cena = {
cena1: function() { num1(); },
cena2: function() { num2(); },
cena3: function() { num3(); }
};
cena['cena' + numero]();
function funcao(){
function num1(){
document.write("1");
}
function num2(){
document.write("2");
}
function num3(){
document.write("3");
}
}
</script>
</body>
</html>
In Firebug the problem is "num1 is not defined".num1, 2 or 3, are inside function "funcao", seems not be a global function.
I make this question because this code example is the closest replica to my real code, and I have to find a solution.
Is there a solution, or should I open a new question for this new problem?
~JC
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.