1

Let say we declare a variable in the global context, like so:

var someVariable = "someValue";

We can always access its value like window['someVariable'] as it is in the global execution context. But, how can we access it value the same way if it is inside some function and not in the global execution context? For e.g.

function someFunction(someParameter) {
  var someVariable = "some value";
  // some code
}

I want to do something like someFucntionContext['someParameter'] or someFucntionContext['someVariable'] to access the value of those variables in the execution context of the someFucntion like I just did for the variable declared in the global context.

UtkarshPramodGupta
  • 7,486
  • 7
  • 30
  • 54
  • Not possible via any built-in method. You'll have to manually expose the information you want to be made public. – CertainPerformance Aug 23 '18 at 06:12
  • `someParameter` is just an alias of the value which you pass into the function `someFunction` as a parameter. It is not declared or initialize anywhere in the syatem so it cannot be accessible the way you are asking as that variable do not exist at all. – Ankit Agarwal Aug 23 '18 at 06:12
  • @AnkitAgarwal Please read my updated question. :) – UtkarshPramodGupta Aug 23 '18 at 06:16
  • You are confusing the execution context with a variable scope. – Bergi Jul 29 '23 at 06:05
  • You are confusing the [execution context](https://stackoverflow.com/questions/9384758/what-is-the-execution-context-in-javascript-exactly/50512406#50512406) with a variable scope. And the latter [is not an object](https://stackoverflow.com/questions/40544709/why-variable-object-was-changed-to-lexical-environment-in-es5). – Bergi Jul 29 '23 at 06:16

3 Answers3

1

That's not possible without returning objects or instantiating the function and accessing the property.

Global variables are automatically a property of the window object, provided you use var and not let or const. Such as root level functions being automatically a method of the window object. But functions do not behave like primitive objects. You need to do something like

function Favorites(){
  return{
     food: "burrito",
     color: "gray"
  }
}

var fav = Favorites();
var favfood = fav.food; //fav['food']

OR

function Favorites(){
  this.food = "burrito";
  this.color = "gray";
}

var fav = new Favorites();
var favfood = fav.food; //fav['food']

And like so

var favfood = window.fav.food;
var favcolor = window['fav']['color']
Abana Clara
  • 4,602
  • 3
  • 18
  • 31
  • Nitpicking: A function is an object. https://stackoverflow.com/questions/3449596/every-object-is-a-function-and-every-function-is-object-which-is-correct – baao Aug 23 '18 at 06:17
  • 1
    I understand the nitpickiness, this is a community where you really have to be nitpicky. Edited. – Abana Clara Aug 23 '18 at 06:18
0

One of the approach could be exposing certain properties of the function itself.

function fn(){
  fn.num = 100;
}
//access fn.num
console.log(fn["num"])

You can control which properties you want to expose from the function itself. For example,

function doSomething(num, addStr){
  //expose num
  doSomething.num = num;
  var str = "Hello ";
  if(addStr){
    str += addStr;
  }
  //expose str
  doSomething.str = str;
}

//set num
doSomething(100);

//access num
console.log(doSomething.num)
console.log(doSomething["num"])

//set num and str
doSomething(200, "Jerry!")

//access str
console.log(doSomething["str"])
gagan
  • 269
  • 1
  • 4
-1
(0, eval)("this").someVariable

The reasons for this are pretty esoteric, but essentially by calling eval("this"), you are evaluating this in the current execution context. And putting it in the (0, eval) construct forces it to execute in the global context, so you always get the correct this - the global object - which allows you to access globals anywhere.

Another way to put it:

function foo() {
  const global = (0, eval)("this");
  global.someVariable = 13;
}

Notably, the 0 can be pretty much anything. The important bit is that the eval is not evaluated in isolation which would tie it to the current execution context, and is instead executed in the global one. With that bit of magic, the expression (a, b) evaluates to b per normal Javascript rules, so you get eval, but it's within the global context.

Also, if you don't need to update the variable you can use a shortcut:

(0, eval)("someVariable")

For an important workaround in some contexts, see How to get the global object in JavaScript?

Rich Remer
  • 2,123
  • 1
  • 21
  • 22