Jump to content

Get variable identifier


dksvertix

Recommended Posts

That doesn't really make sense. If you're reading the variable then you already know the name of the variable you just read.

var garfield = "Gimme lasagne!";
alert("The variable name for " + garfield + " is garfield");

I'm going to need more context to fully understand your real requirements.

 

You probably need a data structure similar to this:

var quotes = [
  { name : "Garfield", value : "Gimme lasagne!" },
  { name : "Person", value : "Something" }
];
  • Like 1
Link to comment
Share on other sites

I have an automated script that reads words from an element to match them with variables which contents will then be fed to a popup script which finally displays the definitions of the words read by the first script.

I Googled for a solution, read Stack Overflow, but the posts also said same thing: not possible (the easy way/requires expert skills), makes no sense.

And yeah, I already have a data structure ready and working. Just still hoped that perhaps there is a simple solution to make that data table smaller by removing "name" values which would be read from identifier instead.

Link to comment
Share on other sites

The dictionary data structure might be what you're looking for. It works like this:

var dictionary = {
  "garfield" : "Gimme lasagne!";
};

for(var item in dictionary) {
  if(dictionary[item] == "Gimme lasagne!") {
    console.log("\"Gimme lasagne!\" belongs to " + item);
    break;
  }
}

More efficiently, you can reverse the values, but they have to all be unique.

var dictionary = {
  "Gimme lasagne!" : "garfield",
  "Something" : "person"
};

if(dictionary["Gimme lasagne!"]) {
  console.log("\"Gimme lasagne!\" belongs to + dictionary["Gimme lasagne!"])
}
Link to comment
Share on other sites

Variable names aren't really part of the data of your program, they are only data for the interpreter running your program. If you need data like that then you have to use some sort of key/value pair like Ingolme is showing. Identifiers are not statically associated with their values, for example:

 

var var1 = 'test';

function someFunc(param1) {
  alert(param1);
}

someFunc(var1);
Inside that function, is it supposed to link up the identifier "var1" with the value in param1? That's not how it works, variable names are really just for you to know what values are represented. They're only used by the interpreter to keep track of the different values, but the interpreter doesn't require meaningful names. Meaningful names are only for the programmer.

 

If your associations are 1-to-1, where Garfield would only ever say one thing (which sounds like what you want if you're trying to use variable names), then a dictionary object will work for you. If your relationship is 1-to-many, where Garfield might say many things, then you need an array of objects like shown in post 2, or a dictionary that stores arrays instead of strings.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...