Create a new unique global variable every time a function runs in javascript

Possible duplicate:
Javascript dynamic variable name

Very simple question. I want to create a new global javascript variable every time a function is called. The variable should contain the identifier of the element so that I can easily access it later.

id = 2347 //this function would be called multiple times, hopefully generating a new global each time function (id) { var + id = something // I want a variable that would be named var2347 that equals something, but the above line doesn't get it. } 

In a later function, I want to access the variable as follows:

 function two (id) { alert(var + id); } 

I am sure that I will have a "dog!" the moment when someone is kind enough to answer this question.

+6
source share
4 answers

What about...

 var store = (function() { var map = {}; return { set: function ( name, value ) { map[ name ] = value; }, get: function ( name ) { return map[ name ]; } }; })(); 

Using:

 store.set( 123, 'some value' ); 

and then...

 store.get( 123 ) // 'some value' store.get( 456 ) // undefined 

Live demo: http://jsfiddle.net/jZfft/

Programmers are strongly advised not to declare global variables, as browsers already send several hundred names to the global namespace. Using a global namespace for your own variables can lead to name conflicts, which can then disrupt the program or some browser features. Creating new namespaces is free, so feel free to do it ...

+6
source

Global variables are properties of the window object, so window.lol and window['lol'] define the global lol variable, which can be accessed in any of these ways. The second, window['lol'] , can also be used with variable names, for example:

 var lol = 123; var name = 'lol'; var content = window[name]; // window['lol'] == 123 

content will now contain 123. Almost everything can be enclosed between square brackets [] , so you can also do this:

 var id = 123; window['prefix' + id] = 'text'; var content = window['prefix' + id]; // prefix123 == text 

Or, in your case:

 var id = 2347; function one(id) { window['var' + id] = something; } function two(id) { alert(window['var' + id]); } 
+4
source

You can save your values ​​in a global hash:

 var g = {}; function (id) { g[id] = something; } function two (id) { alert(g[id]); } 
+3
source

I would say that you really do not want to make many global variables. Most likely, you can simply create one global object or array and attach all other variables to it. In this case, you probably need an object:

 var myIds = {}; function makeSomething(id) { // create something that goes with this id myIds[id] = something; } 

Then, to get this information after a while, you can get it with this:

 var something = myIds[id]; 

The reason for this proposal is multiple. First of all, you want to minimize the number of global variables, since each global is the probability of a name clash with some other script that you could use. Secondly, when tracking a collection of related data, it is best programming practice to keep it in one specific data structure, and not just throw it in a giant global box with all the other data.

You can even create an object that manages all this for you:

 function idFactory() { this.ids = {}; } idFactory.prototype = { makeSomething: function(id) { // create something that goes with this id this.ids[id] = something; }, retrieveSomething: function(id) { return(this.ids[id]); }, clear: function() { this.ids = {}; } }; // then you would use it like this: var myIds = new idFactory(); myIds.makeSomething(2347); var value = myIds.retrieveSomething(2347); 
+2
source

Source: https://habr.com/ru/post/904455/


All Articles