0

How can i get the original name of the variable by it's prototype? I tried MyVar.name but it's not defined.

I have a function for example which get the original variable name.

function varName(v) {
  return v.toString();
}

But when I call it with variable object, it returns [Object object].

varName(window);

I want to make it return the original variable name.

varName(window);
// window
dlvdls2
  • 36
  • 5
  • Does this answer your question? https://stackoverflow.com/questions/33960151/javascript-how-to-get-the-name-of-the-instance/33960197#33960197 – Paulo Jul 02 '22 at 13:31
  • 2
    When you pass a *value* it doesn't come attached with the *variable* it was in. This seems like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – VLAZ Jul 02 '22 at 13:31
  • @Paulo I don't see how it's relevant at all. – VLAZ Jul 02 '22 at 13:32
  • 1
    Values are assigned to variables (sometimes) but there's nothing about that relationship reflected in the value. – Pointy Jul 02 '22 at 13:32
  • Oh, sorry. I misunderstood it. – Paulo Jul 02 '22 at 13:42
  • 2
    Yes, please explain what you need this behavior for, my guess is maybe for something like ["Variable" variables in JavaScript](https://stackoverflow.com/q/5187530), which you can achieve in other ways (as per the link) – Nick Parsons Jul 02 '22 at 13:43
  • This has nothing to do with prototypes. You want to get the name (identifier) of the variable, not the name of the constructor function of the object, right? – Bergi Jul 02 '22 at 20:03

2 Answers2

0

;TLDR

The short but negative answer is it can't be done. Prototyping potentially applies to values stored in a variable, not to names used in code to refer to the variable. If you still have an insurmountable problem please edit the question to provide more details about the actual coding or design issue that remains.

Variable names

A variable name is an identifier used to access the location where the value of the variable is stored. There is no inverse relationship: the value of a variable contains no information about where it is stored.

Because JavaScript supports run time evaluation using variable names in expressions, the compiler and run time engine maintain "bindings" of variable names with the location where they are stored in memory. Such bindings are stored in global and function environment records. The structure and contents of the environment records are, however, not accessible from within JavaScript. You can no more determine the name of variable a function argument might have been stored in prior to being passed to a function by value that you could determine where in a scope chain of environment records a variable being read in code was defined.

traktor
  • 17,588
  • 4
  • 32
  • 53
0

As @traktor said, it cannot be done, but there is an available workaround for you:

Create a function, so that you can name your variable:

foo = function(name) {
   this.name = name;
}

And, while creating one, call it:

var pear = new foo("pear");
Paulo
  • 1,458
  • 2
  • 12
  • 26