We can get the source code of a function with the method toString. We could access the value of the variable busing a regular expression. Thanks to this, we could access the value of the variable without executing the code returning the value of the variable, nor using global variables. You don't technically get the variable , but you can get its value.
If we want to get the value of the variable from outside the execution environment (for example, from another file), we could read the source file and apply a regular expression.
What do you want it for? We really shouldn't care. If you're curious, here's the demo using the toString.
let a = function() {
const b = 20;
}
// Con el método `toString` accedemos al código fuente
// de la función
var codigoFuenteDeFuncion = a.toString();
// Aplicamos una expresión regular para obtener el valor
// de la variable
var valorDeVariableInterna = /const\sb\s\=\s(.+);/.exec(codigoFuenteDeFuncion)[1];
console.log(valorDeVariableInterna);
The way in which you want to obtain the variable is incorrect, since you want to equalize a variable (from the int that comes before it, I imagine that you want it to be an integer, although that declaration is also incorrect) with a function.
What you should do is declare a variable for example like this:
var a;
On the other hand, if you want the function to return an integer, we must tell it to do so by adding the return and a name so that we can call it and give it the equivalence to our initial variable, in other words, one way of expressing what has been explained would be:
var a = retornar();
function retornar() {
var b = 20;
return b;
}
console.log(a);
console.log(retornar()); // esta sería otra forma
Several already answered you but complementing, if you want to necessarily use the notation that you put
a().b
you can do the following:or using ES6
You can declare a "global" variable, which will be modified with the value of
b
when the function is calleda
:You could also return the value of
b
when calling the function:It should be enough for you to return the value; That is, your code looks like this:
a
that, being matched with the function, can access the value that it returns, writing it like this:a()
We can get the source code of a function with the method
toString
. We could access the value of the variableb
using a regular expression. Thanks to this, we could access the value of the variable without executing the code returning the value of the variable, nor using global variables. You don't technically get the variable , but you can get its value.If we want to get the value of the variable from outside the execution environment (for example, from another file), we could read the source file and apply a regular expression.
What do you want it for? We really shouldn't care. If you're curious, here's the demo using the
toString
.The way in which you want to obtain the variable is incorrect, since you want to equalize a variable (from the int that comes before it, I imagine that you want it to be an integer, although that declaration is also incorrect) with a function.
What you should do is declare a variable for example like this:
On the other hand, if you want the function to return an integer, we must tell it to do so by adding the return and a name so that we can call it and give it the equivalence to our initial variable, in other words, one way of expressing what has been explained would be: