How to list the variables defined within a scope in JavaScript?

8

How do I know / list which variables have already been defined within a scope, whether global or local?

For example if I set var x, y, z = 10 , the result of a possible command to list the already defined variables should be something like array with their names: ["x", "y", "z"] .

Is it possible to do this in JavaScript?

In R, for example, this command would be ls() and would return a vector of strings with variable names.

    
asked by anonymous 03.05.2014 / 23:22

3 answers

6

This is not possible in JavaScript, because objects where variables are stored ( environment records ) are not available to the language user except in the case of the global object. But even then, listing the properties of the global object will include all the global variables, not just the ones you've created. For example:

var a = 10;
for(i in window) {
    if(window.hasOwnProperty(i)) console.log(i)
}

The output will include its a variable, but also includes document , localStorage , name , etc.

You can partially work around this problem by passing the% object arguments from one function to another. For example:

function soma(a, b) {
    return a + b;
}

function subtrai(a, b) {
    return a - b;
}

function somaOuSubtrai(a, b) {
    // Transforma os argumentos de objeto para array, esperada pelo apply.
    // Não é 100% necessário, mas garante compatibilidade com implementações antigas
    var args = Array.prototype.slice.call(arguments);

    if(a >= b) {
        return subtrai.apply(null, args);
    } else {
        return soma.apply(null, args);
    }
}

somaOuSubtrai(1,3); // 4
somaOuSubtrai(3,1); // 2

Another alternative is to package your variables as properties of an object (a namespace ), as suggested by @Sergio. In the end, you may end up using a combination of these strategies.

    
03.05.2014 / 23:47
7

As @bfavareto also mentioned this is not possible in Javascript. However, if you plan your code well using a placeholder you can do this verification. However, variables defined within functions are not accessible to outer scopes.

var namespace = {
    minhafuncao1: function () {

    },
    minhavariavel1: 100
}

for (variable in namespace) {
    console.log(variable);
}

Gives:

minhafuncao1 
minhavariavel1 
    
03.05.2014 / 23:51
3

Yes and no . No in almost every situation. Yes , but only to a limited extent if you want to check the global reach. See the following example:

var a = 1, b = 2, c = 3;

for ( var i in window ) 
{
    console.log(i, typeof window[i], window[i]);
}

That generates, among 150 + other things, the following:

getInterface function getInterface()
i string i // <- Veja aqui!
c number 3
b number 2
a number 1 // <- Outro...
_firebug object Object firebug=1.4.5 element=div#_firebugConsole
"Firebug command line does not support '$0'"
"Firebug command line does not support '$1'"
_FirebugCommandLine object Object
hasDuplicate boolean false

So it is possible to list some variables in the current scope, but it is not reliable, efficient, or easily accessible .

The best question is why do you want to know what the variables are in scope?

Original response in English

    
03.05.2014 / 23:55