Home javascript How do I "dereference" a variable's content for use as a variable name?
Reply: 0

javascript How do I "dereference" a variable's content for use as a variable name?

user1209 Published in July 17, 2018, 7:49 am

I want to reduce the I/O overhead of fetching information from the server using XMLHttpRequest() or ActiveXObject(), as appropriate, by making a general-purpose function for doing the fetch which then stores the fetched data using sessionStorage.<variable>. The trouble is, I don't know in advance what the variable names all are, and I intentionally don't want to know.

I was thinking that if there is some way to "dereference" a variable, like we can easily do in BASH, for example, this would be trivial. For example, if the fetched data was in newData, and the name of the file it was fetched from is in dataFile, and if the dereference syntax was, say $(<variableName>), then one could write code like this to store and fetch the data:

//Store the data:
sessionStorage.$(dataFile) = newData;

//Fetch the data:

var storedData = sessionStorage.$(dataFile);

Get it? ... OK, now, how do I ACTUALLY do this?!

...The only other way around this I can see is VERY clumsy - make arrays, one with name, another with values - there MUST be an easier way! TIA.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.354679 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO