1

Some people beieved that this is a duplicate of Variable name as a string in Javascript, this is incorrect. If you read the test below, you would understand that the methods provided were not satisfactory since this variable HAS to be a variable, it cannot be an object property.

I have been looking around for "How to convert variable name to string", but all I got was(as explained in How to convert variable name to string in JavaScript?) objects and things that only worked in the global scope.

Is there a way to convert a variable name DIRECTLY into a string, without doing weird stuff.

like:

var alertThis = "dont alert this"
alert(alertThis.forVarToString());
// This should return "alertThis"

This functionality is quite necessary for what I am doing.

Community
  • 1
  • 1
Julian Avar
  • 476
  • 1
  • 5
  • 24
  • 6
    What exactly are you doing? I'm pretty sure it's not necessary. – Bergi Dec 09 '15 at 01:58
  • Possible duplicate of [Variable name as a string in Javascript](http://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript) – JCOC611 Dec 09 '15 at 02:05
  • @JCOC611 This is no duplicate because as I said, the answers given dont help with what I'm trying to accomplish(sense I can't make my variable an object property) – Julian Avar Dec 09 '15 at 02:07
  • 3
    Well those are the *only* ways to accomplish it (either transverse the window object and try to find your variable, assuming you drop the `var`, or you use an object instead). Otherwise, you could try sharing the *why* of this question so we can help more. – JCOC611 Dec 09 '15 at 02:09
  • 5
    This is still an XY problem. In almost every possible scenario, if you are trying to call `alertThis` and have it output a string, you already would need to know that variable's name in order to write the code for it, and `'alertThis'` would be obvious. – Claies Dec 09 '15 at 02:11
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/97358/discussion-on-question-by-julian-avar-explanation-converting-variable-name-to-st). – elixenide Dec 09 '15 at 04:45

1 Answers1

1

I don't think that's generally possible (it's an internal value stored in some symbol table). Global variables are of course exposed on this (e.g. window), so you can determine whether a given string is a global identifier:

var hello = 'Hi';
Object.keys(window).indexOf('hello') >= 0;
//=> true

As regards the method for converting any identifier to its string representation (not what it's referring to), that wouldn't be very useful, because to call that method, you would have to first have that identifier, in which case you would already know its name.

Hunan Rostomyan
  • 2,176
  • 2
  • 22
  • 31
  • Sorry if I'm asking too much, but what does `.indexOf('hello') >= 0;` do? – Julian Avar Dec 09 '15 at 02:15
  • @julianavar `Object.keys(window)` is a list of strings, so `indexOf('hello')` returns the index of `'hello'` in that list if it's found. Otherwise returns `-1`. So by checking for `>= 0`, we're checking if `window.hello` is declared. – Hunan Rostomyan Dec 09 '15 at 02:18
  • Yeah, that wont do... – Julian Avar Dec 09 '15 at 02:22
  • And by the way, this feature is quite useful for what I am trying to do, it might not seem useful to you, but that is only because maybe you haven't seen a need to do this yet. I have and that is why I ask. – Julian Avar Dec 09 '15 at 02:29
  • @julianavar JCOC611 made a *key* suggestion: preprocess your code instead. I suspect that's what you're trying to do, except you're letting the js engine do the processing for you and then you ask it for values. Instead, parse your code, make a list of identifiers and then do stuff with them before handing the code off to the interpreter? – Hunan Rostomyan Dec 09 '15 at 02:31
  • But how would I do that? I'm sorry if I'm not an expert in this topic, but what are you talking about? What is a preprocessing and how would it work. How would I accomplish what I'm trying to do? – Julian Avar Dec 09 '15 at 02:34
  • @julianavar To preprocess, you'd simply parse the code and store what follows `var` in some table called 'identifiers'. Then you hand the code intact to the compiler, which does its thing with it. One way of accomplishing this goal is to use a javascript parser (e.g. *Esprima*) and let it generate an abstract syntax tree, from which you can then easily extract the identifiers (the tree will likely be in json format). – Hunan Rostomyan Dec 09 '15 at 02:42
  • Or, you know, explain _why_ you need to do this and perhaps we can suggest an alternative other than preprocessing your JS. – Andy Dec 09 '15 at 02:43
  • @Andy ok, but If i do post my question, you have to understand that it is a very specific situation – Julian Avar Dec 09 '15 at 02:47
  • 2
    Just add the relevant details to your question, or delete this one and ask another. You've got a lot of pushback here because it looks like like an XY problem: ["That is, you are trying to solve problem X, and you think solution Y would work, but instead of asking about X when you run into trouble, you ask about Y."](http://meta.stackexchange.com/a/66378) – Andy Dec 09 '15 at 02:50
  • @Andy ok, but then you do understand that the question I would write would be quite long a detailed. I am trying to do a TON of stuff, and I was pretty sure that I could get the answer here, if you think that you can take an extremelly long explanation(which is all necesarry in order to correctly address the problems), then tell me and I will post a new qeustion. I just want to make sure that the question I will make will be addressed and will not just be ignored(which happens a lot in specific code). – Julian Avar Dec 09 '15 at 03:26
  • I can't tell you it will be addressed. Would the response be worse than the one you've got for this question? Probably not. – Andy Dec 09 '15 at 03:29
  • @Andy ok, then I will post my new question as soon as finished. :) Thank you for your support! – Julian Avar Dec 09 '15 at 03:35
  • It has taken over an hour and I HAVENT gotten anywhere, the question ends up being to long. I'll just have to leave it as it is... – Julian Avar Dec 09 '15 at 04:29