4

This is NOT about how to get keys or values

In the example below:

var exampleObject = {startIndex: 1, stopIndex: 2};

How can I get the exact name "exampleObject" of this object? I have tried exampleObject.constructor.name but all I got was "Object" as the result.

6
  • 1
    Not sure if this is achievable the end of the day you can say var exampleObject2 = exampleObject ... So that would net an array of results. The var is just a reference to somewhere in memory... Commented Feb 15, 2019 at 22:12
  • 3
    If you shared why you want to get the name (identifier) of this variable, we could point you in a better direction. There should be no reason why you would need to know this identifier. Commented Feb 15, 2019 at 22:13
  • 3
    This is not possible. Why do you need to do this? If you need to get the name of the variable, why don't you store the object in another object and give it a name? Commented Feb 15, 2019 at 22:13
  • If you cant use keys or hashes than why not just store the object name as a property in the object called name? Maybe you can find an answer here: stackoverflow.com/questions/4602141/… Commented Feb 15, 2019 at 22:22
  • 1
    If you take one thing from these comments and answers, it's this. There is no reason why you would need to know the name of this variable. If you think there is, then you are doing it wrong. Like I said, if you let us know your end goal, we may be able to offer better solutions to what you are trying to do. Commented Feb 15, 2019 at 22:32

2 Answers 2

11

You cannot.*

First of all, more than one variables can point to the same object. Variables are only references, not containers. There is no one true name for an object. Here's an example:

function makeExample() () {
  var x = {startIndex: 1, stopIndex: 2};
  var y = x;
  return y;
}

var z = makeExample();

In the above code, what should the name of the object be? x, y, z? All of these variables don't contain a copy of the object, they point to the same object. exampleObject is the name of the variable, not the name of the object.

A variable name is just a label to be used by the programmer, not by code. That is not the same thing as a property, which is data stored inside an object and identified by a key which is either a string or a symbol. If the object needs to have a name, then it should be part of its own properties:

var exampleObject = { name: "exampleObject" };

*Technically, any variable created with var in the global scope of a script that is not executed as a module will be added to the window object. That is a relic of the past and you should not rely on this - in fact, modern JS code should use let to create variables which do not have this behavior.

Sign up to request clarification or add additional context in comments.

3 Comments

A good case would be a minifier. A minifier will rename all of your variables to be as short as possible, meaning getting the actual name would be impossible anyway.
@messerbill Note that functions actually do have a name, which can be read with myFunction.name
yes it was a completley other case hence why i have deleted the comment again. the topic was about how to get the name of the calling method.
0

The only way I know of doing this is with this custom function:

var returnObjectName = function (object) {
  var objectName;
  for(var i in window){
    if(window[i] === object) {
      objectName = i;
    }
  }
return objectName;
}
(Note this function might mess up if there is an object with the same value as the object name you are trying to find!)

If I did something wrong do not just downvote my answer but also tell me what I did wrong.

6 Comments

OP is asking to get the name, your examples give the name and assume they know it.
I won't down vote since you're a new contributor, but you should consider taking on board these comments and perhaps deleting this answer to avoid down votes.
Now I have fixed my response to answer the question.
This will only work if the object was created in the global scope. Also what if you have two or more aliases of the same object?
jsfiddle.net/7jfxw89h i think this problem cant be really solved....since it seems not to give any advantages
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.