0

I've looked everywhere, and it seems like the kind of thing that should be simple, so forgive me if this IS simple or violates best practices or I just missed it.

I want to be able to convert an object reference into a string. Not the object being referred to, but the reference itself.

Let's say I have a method nested in an object, for example:

a.b.c.d()

where a is the object and d is the method, how do I retrieve the string:

"a.b.c.d" ?

To be a little more specific, I want to pass the object reference as an argument to a function and be able to convert it to a string representing the key path:

function convertPath ( a.b.c.d ) {
    ...
}

could return the string "a.b.c.d". (I'd perform some other operations with it instead of just returning it, but I'm keeping this simple.)

Methods like "toString", etc. return the actual function being referenced as a string.

I've seen plenty of references to do the opposite (pass a string and convert it to an object reference), which would create other complications for me, but not this.

Any help would be appreciated. Thanks!

  • 1
    According to [this stackoverflow post](https://stackoverflow.com/questions/1789892/access-parent-object-in-javascript), it's not possible to access the parent object from within a nested object – Jannes Carpentier Aug 06 '20 at 21:05
  • Thanks for answering, but I'm afraid I don't understand. I'm not trying to access the parent object. I'm just trying to take the argument being passed (a.b.c.d) and reproduce it as a string ("a.b.c.d"). – Pablo Carson Aug 06 '20 at 21:39
  • 2
    If you pass `a.b.c.d`, you only have this `d` object. There is no information about `a b c` so you cannot convert this path into a string. You'd need access to `a` in order to know the path to `d` is `a.b.c.d` – Jannes Carpentier Aug 06 '20 at 21:42
  • What do you need to do this for? If you explain why you think you need `"a.b.c.d"`, you'll probably get a much more useful answer. – Patrick Roberts Aug 06 '20 at 21:44
  • I need access to both string and object reference. Some keys in the object references in my code are represented by abstractions that return a value. So receiving a string as an argument and converting it to the object ref gets a little complicated. I can work it out but I thought it might be simpler to just receive the object ref and convert it to a string. But I get Jannes Carpentier's response - it seemed to me that if you're passing the keypath as an argument, the info about a b and c is right there but I get that that's not how it works. I'll go the other route. Thanks to you both. – Pablo Carson Aug 06 '20 at 22:48
  • You COULD pass "a.b.c.d" to the function as, say `path`, and to actually get d, use `eval(path)`... You probably SHOULDN'T do it... (and to eval, say, "a.b.c" from `path`, you could: `path=path.split(".");path.pop();path=path.join("."); eval(path);`) Repeating other's question: Why? – iAmOren Aug 07 '20 at 01:41
  • I tried to answer the question "why" in the response to Patrick's question, if that's not clear it would be helpful for me to understand if I didn't describe it properly. I went the route I mentioned there instead, passing in the argument as a string and converting it to an object reference. It's stated practically everywhere that eval should be avoided so I excluded it form consideration. Thanks for your input. – Pablo Carson Aug 07 '20 at 16:51

0 Answers0