How to extract a variable from within a function

Asked

Viewed 650 times

4

Suppose I use the following function in .js... And inside it there are 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);
  • Not exactly the answer to your question, but that doubt, can add you interesting information. (:

  • Related: http://answall.com/a/58621/6077

6 answers

4


there are two ways

1 - Declaring out. That way the code becomes 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. The variables declared with var or accessed in the global scope (out of functions) are object properties window, then you can assign that way

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

4

The variables created within the function exist only in this scope, that is within the function. To access them both inside and outside, they must be created outside the function within the general scope:
Example:

https://jsfiddle.net/5zc3yqks/

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 the ON the variable does not exist yet, so it will create and set the value, then it leaves the scope of the ON and creates the variable again, taking the value that was associated.

3

Within a function 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.

2

When you need to declare a global variable within a function, you must specify the depth from which 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>
    <script type="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>

0

You will never be able to access the values externally for a Lambda Function, of the kind of jQuery that run everything internally, however, you can create a subfunction that has an external output according to 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 change you make to this variable will only be executed internally, to have an output, you need to have an external method that captures that output internally.

-1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.