How to extract a variable from within a function

4

Assuming I use the following function in .js ... and inside it has some variables.

$(document).on("load", function(){
    var Variavel1= "um";
    var Variavel2= "dois";
    var Variavel3= "tres";
    var Variavel4= "quatro";
})

How do I use any of these variables outside of this function?

$(document).on("load", function(){
    var Variavel1= "um";
    var Variavel2= "dois";
    var Variavel3= "tres";
    var Variavel4= "quatro";
})

alert(Variavel3);
    
asked by anonymous 27.08.2015 / 19:23

6 answers

4

There are two ways

1 - Declaring out. This way the code is simpler to understand and you can control the scopes explicitly.

var foo; // declaração fora
function teste() {
  foo = 'valor'; // sem "var"
}
teste(); // executa função
alert(foo); // exibe "valor"

2 - Assigning by window . Variables declared with var or accessed in the global scope (out of functions) are properties of the object window , so you can assign it in this way

function teste() {
  window.foo = 'valor'; // atribui como propriedade de "window"
}
teste(); // executa função
alert(foo); // acessa sem "window." - exibe "valor"
    
28.08.2015 / 19:18
4

Variables created within the function exist only within that scope, ie within the function. To access them both inside and outside, one must create them outside the function within the general scope ie:
Example:

link

As you can see in my example it works, but in your case, you have to see the following, the instructions that are inside the "on" are executed before the declaration of the variable itself. That is, in the scope of ON the variable does not even exist, hence it will create and set the value, then it exits the scope of the ON and creates the variable again, taking the value that was associated.     

27.08.2015 / 19:25
3

Within a role you have a local scope .

To access a local scope variable within another location, the only way I see it is returning the variable you want . So you call the function and get the variable.

Or else, , you declare the variables outside the functions. Global Scope . So you can access them in any function.

    
27.08.2015 / 19:26
2

When you need to declare a global variable within a function, you must specify the depth of where the object will be instantiated.

To declare within the global scope, declare with the prefix window.

See the example below:

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><scripttype="text/javascript">
    $().ready(function() {

        window.variavel1= "um";
        var variavel2= "dois";
        var variavel3= "tres";

    });

    function foo()
    {
        console.log('test: '+ variavel1);
    /*
observe que variavel1 está no escopo global. 
tente executar console.log('test: '+ variavel2); e obterá erro de variável indefinida pois está dentro do escobo da *lambda function*.
*/
    }
    </script>
    </head>
    <body>

    <input type="button" value="click me!" onclick="foo();" />

    </body>
    </html>
    
28.08.2015 / 19:29
0

You will never be able to access the values externally for a Lambda function , of those of the jQuery type that perform everything internally, however you can create a subfunction that has an external output as in this example:

 $(document).ready(function(){
    var variavel1= "um";
    var variavel2= "dois";
    var variavel3= "tres";
    var variavel4= "quatro";
    variaveisSaida(variavel1,variavel2,variavel3,variavel4);
  });

function variaveisSaida(val1,val2,val3,val4) {
    console.log('estou aqui fora: '+val1+','+val2+','+val3+','+val4);
    alert(val1+','+val2+','+val3+','+val4);
}

You can also declare a variable with a value externally and use it within its scope, but any changes you make to this variable will only be performed internally, to have an output, you must have an external method that internally captures this output.

    
27.08.2015 / 22:25
-1

Declare the variables outside the function ... they are only recognized within the function because they were declared inside the function

    
28.08.2015 / 19:48