2

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.

  • 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... – JGFMK Feb 15 '19 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. – Cjmarkham Feb 15 '19 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? – Indiana Kernick Feb 15 '19 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: https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript – cb64 Feb 15 '19 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. – Cjmarkham Feb 15 '19 at 22:32
  • like @Kerndog73 said, if you really need that information do something like `var exampleObject = {name: "exampleObject", startIndex: 1, stopIndex: 2};` – messerbill Feb 15 '19 at 22:44

2 Answers2

10

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.

Domino
  • 6,314
  • 1
  • 32
  • 58
  • 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. – Cjmarkham Feb 15 '19 at 22:29
  • @messerbill Note that functions actually do have a name, which can be read with `myFunction.name` – Domino Feb 15 '19 at 22:39
  • 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. – messerbill Feb 15 '19 at 22:41
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.

Jaime Argila
  • 408
  • 3
  • 13
  • OP is asking to get the name, your examples give the name and assume they know it. – Cjmarkham Feb 15 '19 at 22:24
  • 1
    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. – Cjmarkham Feb 15 '19 at 22:25
  • Now I have fixed my response to answer the question. – Jaime Argila Feb 15 '19 at 22:44
  • 1
    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? – abadalyan Feb 15 '19 at 22:46
  • https://jsfiddle.net/7jfxw89h/ i think this problem cant be really solved....since it seems not to give any advantages – messerbill Feb 15 '19 at 22:48
  • Read my note and is this not good enough just because it only works with global objects and the question was referring to a global object. – Jaime Argila Feb 15 '19 at 22:49