Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
JavaScript - Get variable original name by it's prototype

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
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

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");
about 4 years ago · Juan Pablo Isaza Report

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!