¿Cómo acceder a la variable desde otra función? alerta de la función 1 toma la suma de las 2 variables de la función 2
fun1(); function fun1() { alert("var two + var three = " + two + three); } function fun2() { var one = 1; var two = 2; var three = 3; var four = 4; }Defina estas variables globalmente fuera de ambas funciones (una muy mala práctica para cualquier programa del mundo real, pero lo suficientemente adecuado para un ejemplo), o proporcione como argumentos para la función de alerta.
var one, two, three, four; fun1(); function fun1 () { fun2(); alert("var two + var three = " + two + three); } function fun2 () { one = 1; two = 2; three = 3; four = 4; } fun2(); function fun1 (one, two, three, four) { alert("var two + var three = " + two + three); } function fun2 () { fun1(1, 2, 3, 4); }Esto no es posible acceder a una variable desde fuera de Scope
si desea acceder a él desde más funciones, hágalo global. Como abajo.
fun2(); fun1(); var two = 2; var three = 3; function fun1() { alert("var two + var three = " + two + three); } function fun2() { var one = 1; two = two + 2; three = three +3; var four = 4; }