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
  • 6,416
  • 6
  • 25
  • 49
  • 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

2 Answers2

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,232
  • 3
  • 18
  • 30
  • 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
  • 259
  • 1
  • 4