0

As per the title is it possible to retrieve a variable name from within a function.

For example i am passing a selector object into a function

get SAMPLE_ELEMENT () { return $('h2=example') }

This is passed into a function

verifyElementTextTranslated (element) 

What i would like to get back is SAMPLE_ELEMENT as this will be used to compare against a json file to extract the correct translation.

I've tried element.toString() but that gives me an [object, object] back, trying just to return element gives me the entire object back obviously.

Not sure if it is actually possible because if my understanding is correct as variable names aren't passed into the function?

Thank you

Kyle
  • 133
  • 7
  • 1
    It is not possible. (What if the value passed isn't in a variable?) What problem are you trying to solve that (seems to) require this? – Scott Hunter Feb 07 '23 at 17:06
  • Not sure what you mean? As in pass the selector string and don't store it in a variable? – Kyle Feb 07 '23 at 17:08
  • 1
    I'm saying it is possible to pass a value that is not stored in a variable, in which case there is no name to be gotten. – Scott Hunter Feb 07 '23 at 17:27
  • yeah obviously, but thats not how my framework is setup. Which is why i was exploring the more complicated method. But thank you for reviewing, appreciate your time and the confirmation that what i want isn't feasible! – Kyle Feb 07 '23 at 17:29
  • 2
    `SAMPLE_ELEMENT` is a property name (in particular, of a getter property), not a variable name. Are you doing `const element = something.SAMPLE_ELEMENT;` somewhere? Please give more context! – Bergi Feb 07 '23 at 17:32
  • In my function i want to get `const elementName = element.toString()` or something equivalent to that – Kyle Feb 07 '23 at 17:56
  • You can pass the name as its own parameter. `verifyElementTextTranslated(parent, propertyName) { var element = parent[propertyName]; /* now you have the element and its property name */ }` – Raymond Chen Feb 07 '23 at 18:04
  • In the example above what would be the parent and what would be the property name? – Kyle Feb 07 '23 at 18:08

0 Answers0