1

In JavaScript I want to make a function which takes an argument and in this function a variable will be created whose name will be the value of the argument.
For example if user pass "jack" in the argument then in function I want a variable whose name is jack like:

var jack = "";

5 Answers5

1

Typically, you won't need to do this. As Bergi pointed out, a local variable would usually suffice. However, in the event that you do need to use this technique, you could give the property to the window object:

function setVariable(userPassedString);
    window[userPassedString] = "";
}

Don't use eval for this, ever, no matter how tempted you are to.

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • 1
    And dont use the global object for this, ever, no matter how tempted you are :-) – Bergi May 15 '13 at 14:09
  • Do you have another solution, Bergi? – Elliot Bonneville May 15 '13 at 14:09
  • Tbh, `window` isn't that much better. – Ja͢ck May 15 '13 at 14:09
  • @Elliot: It depends on the exact use case (which OP did not give), but usually a local object will suffice. – Bergi May 15 '13 at 14:10
  • 1
    Why the downvote? Is there another, better method? @Bergi, I don't know enough to tell the OP what he should and shouldn't do, so I'm just telling him how to do what he asked. – Elliot Bonneville May 15 '13 at 14:10
  • @Elliot: Then first comment on his question and ask what you need to know for a proper answer, instead of proposing a solution that likely does not fit his needs. You hardly ever need dynamic global variables with unknown names. (not my downvote btw) – Bergi May 15 '13 at 14:16
  • That'd be a good first step, but two comments of that nature had already been posted, and neither received an answer. My doing so as well would have been redundant... – Elliot Bonneville May 15 '13 at 14:19
  • Is it safe to use window[] object? public mentioned that using `eval` creates `security holes`, how safe is it using `window` object? – Rajshekar Reddy Sep 22 '14 at 11:07
1

Creating local variables via a function is typically a bad idea. You could accomplish something similar by passing a local object around, e.g.

function setVar(o, name, value)
{
    o[name] = value;
}

Then, inside your local scope:

var o = {};

setVar(o, 'jack', 123);

// o.jack contains 123

In this way, if the need would really arise (this is rarely required) to introduce global variables in this manner, you can always call the function like this:

setVar(window, 'jack', 123);
// now window.jack == jack == 123
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

The best that you can do about is to create an object and assigns the variable name to the keys of the created object like this -

var myvar={};
function create(var){
    myvar[var]='values';
}
spaceman12
  • 1,039
  • 11
  • 18
0

To prevent using the window global array, you can create a local array which holds your variables.

function doSomething(var) {
  var vars = {};
  vars[var] = value;
}
Guido Bouman
  • 3,155
  • 4
  • 22
  • 33
0

You could always use a dictionary. Here is a very simple stub:

function Dictionary(){
    var container = {};

    this.set = function(key, value){
        container[key]  = value;
    }

    this.get = function(key){
        return container[key];
    }
}

var vars = new Dictionary();
vars.set('foo', 'foo rocks');
vars.set('bar', 'bar rocks too');

console.log(vars.get('foo'));
console.log(vars.get('bar'));
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73