Access variable inside and outside a function

Asked

Viewed 18,066 times

4

It is possible to access a variable inside a function outside of it?

I would like to use a variable that was created in a function outside of it, but without returning by return of function.

In PHP we can do this

$variavel = 123;

function qualquerCoisa(){
    global $variavel;

    echo $variavel;
}

I want something similar in Javascript

function qualquerCoisa(){
    var teste = 1;
}

qualquerCoisa();

console.log(teste); // <- Aqui, dá erro porque a variável está dentro da função.
  • 1

    you can return or use the global.

  • 1

    What you call exporting?

  • He came in the middle of it here: http://answall.com/questions/114377/como-account-attributed-a-um-element-que-foi-creature-com-javascript/114407?noredirect=1#comment238687_114407

2 answers

12


In Javascript the scope of variables depends on the reserved word var. If a variable is created without that word it will be global, otherwise local.

Ex.:

function setNome1(nome) {
  nome_usuario = nome; // nome_usuario foi criada no escopo global
}

function setNome2(nome) {
  var nome_usuario = nome; // nome_usuario foi criada no escopo local
}

setNome1('lucas');
alert(nome_usuario); // lucas
setNome2('felipe');
alert(nome_usuario); // lucas

In the same way that functions can create variables in the global scope, it is possible to access them.

Ex.:

var nome = 'matheus';
function getNome() {
    // A variável nome não existe neste escopo.
    // O interpretador irá buscar em no escopo do qual essa
    // função foi definida. Caso não exista, irá buscar no
    // escopo superior, e assim por diante.
    // Caso a variável não exista será lançado um erro
    // ReferenceError: nome is not defined
    //
    return nome; // 'matheus'
}

alert(getNome()); // 'matheus'

Any function can also change the value of a variable defined in the scope of which it was defined, or at the top.

Ex.:

var nome_usuario = 'douglas';
function setNome(nome) {
    nome_usuario = nome;
}

alert(nome_usuario); // douglas
setNome('emilia');
alert(nome_usuario); // emilia

3

In PHP, to export the variable itself would have to declare it global. It’s the only way, but don’t do this. In general global variables should only be used in PHP very specific situations, and certainly not to export a variable.

In several languages a global variable is something bad given the size of the application. in PHP is not so bad because it runs as script, Still, you have to be careful. Although not so problematic there are better solutions and it is almost impossible to have a good justification to use a global variable for this case. Then look for a better solution.

If you just want to return the value, then a simple return resolve. Eventually if you need to return more than one value there is the solution to use a benchmark or even returns a data structure with more than one value (a array, for example).

Browser other questions tagged

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