0

Given a Javascript object with the method myobject.mymethod the name of the method can be retrieved for printing or whatever using myobject.mymethod.name.

How do I achieve the same thing for property myobject.myproperty.

UPDATE: Specific scenario.

I have an object from a third party library that defines a load of constant values used throughout its api

obj = {
  CONST1 = 1;
  CONST2 = 2;
  CONST3 = 3;
  // ...
}

I'm handling events that are called with these values and want to log what each event is called with. The raw values aren't useful in a log, so I want to log the constant names, ideally without a switch statement mapping values to hardcoded strings or having to define my own lookup table.

The library object has a load of other crap defined on it too, so just looking up the property using the value is not a reliable solution.

Neutrino
  • 8,496
  • 4
  • 57
  • 83
  • 3
    you can get all the keys, using Object.keys(). For more info see this https://stackoverflow.com/questions/4260308/getting-the-objects-property-name#:~:text=No%2C%20an%20object%20could%20be,way%20of%20knowing%20its%20name. – Harmandeep Singh Kalsi Jun 30 '20 at 09:09
  • 1
    You cannot. In fact the `myobject.mymethod.name` is not necessarily the property name, for example `myobject = { mymethod: function otherName() {} }`. My question is what is the goal here? You are *already* accessing the property by name, ergo you know what the name is. Why would you need to try and figure it out again? – VLAZ Jun 30 '20 at 09:13
  • I not understand what do you want to achieve, what is `specific JavaScript property`? do you want names of object properties? – Sachink Jun 30 '20 at 09:21

1 Answers1

0

You can list your properties with Object.keys(). So for example if you need list of props with their values, you can do something like this:

let a = {prop: "foo"};

console.log( Object.keys(a) ); // prints all prop names

Object.keys(a).map( (i) => console.log("property:", i, " has value: ", a[i]) ); // prints all props with corresponding values

Updated answer: Use debugger to find out which values are used inside program. https://developers.google.com/web/tools/chrome-devtools/javascript/breakpoints

Kudlas
  • 659
  • 7
  • 24
  • A method is going to be listed here aswell. – SirPilan Jun 30 '20 at 09:21
  • Yeah, that is true. Simple condition should be added, if that is problem. – Kudlas Jun 30 '20 at 09:24
  • I don't see how this answers the question. Using this example, and assuming `a` has other properties, how do I get just the name of property `prop`. It's name among a list of all the other property names is not what I'm after. – Neutrino Jun 30 '20 at 09:56
  • @Neutrino what *are* you after? I do not understand the question - you already have the name of a property since you're fetching it by said name. But then you want to get the name of the property without using the name of the property. That is a meaningless operation. When you fetch something from an object, it's always a *value* and there is no link between a value and the label you used to reference it - be that a variable, an object property, a parameter, etc. – VLAZ Jun 30 '20 at 10:01
  • Given the name of a property, how do I convert it into a string. – Neutrino Jun 30 '20 at 10:05
  • @Neutrino You cannot. You can do the opposite - take a string and get the property using string. – VLAZ Jun 30 '20 at 10:07
  • Ok. Thanks VLAZ. I'm quite surprised this isn't possible considering that you can do nearly everything in Javascript with strings. I guess I'll just have to create my own lookup map. – Neutrino Jun 30 '20 at 10:13
  • @Neutrino Again, what's the expectation? Why do you need a lookup map? You *already* have the property name for the property, since that's how you're accessing it. – VLAZ Jun 30 '20 at 10:15
  • You can use javascript debugger to find out values used in called functions. – Kudlas Jun 30 '20 at 10:28