admin管理员组

文章数量:1306261

I need to dynamical create a variable off the value of a variable from an argument I am pulling in from a script. But I first want to check if it already exists so I don't overwrite it, if it is already set.

I am leveraging eval to expand the value of the variable and tag it onto a variable suffix but no matter what I use to check for the undefined I am getting a reference error on the check.

Below is the simple code snippet

chkUser = args[1];

if(typeof eval(chkUser+"lastseen")==="undefined") ;

I've also tried put the eval in parens and putting undefined in ' ' and pulled them pletely and switching typeof to the JavaScript standard undefined as such

if( eval(chkUser+"lastseen")=== undefined) ;

but I always get ReferenceError: robertlastseen is not defined or what ever chkUser expands out to.

I need to dynamical create a variable off the value of a variable from an argument I am pulling in from a script. But I first want to check if it already exists so I don't overwrite it, if it is already set.

I am leveraging eval to expand the value of the variable and tag it onto a variable suffix but no matter what I use to check for the undefined I am getting a reference error on the check.

Below is the simple code snippet

chkUser = args[1];

if(typeof eval(chkUser+"lastseen")==="undefined") ;

I've also tried put the eval in parens and putting undefined in ' ' and pulled them pletely and switching typeof to the JavaScript standard undefined as such

if( eval(chkUser+"lastseen")=== undefined) ;

but I always get ReferenceError: robertlastseen is not defined or what ever chkUser expands out to.

Share Improve this question edited Jun 2, 2014 at 19:03 jgillich 76.4k7 gold badges60 silver badges88 bronze badges asked Jun 2, 2014 at 19:00 user3577459user3577459 611 gold badge1 silver badge2 bronze badges 4
  • Run the typeof call inside the eval(). if(eval('typeof '+chkUser+'lastseen') === "undefined") – gen_Eric Commented Jun 2, 2014 at 19:05
  • 1 Have you considered storing these as properties of an Object rather than as separate variables? Bracket notation is usually simpler than eval() and won't throw ReferenceErrors. Or, pass a user object rather than the variable prefix as args[1] -- if (typeof args[1].lastseen === 'undefined'). – Jonathan Lonowski Commented Jun 2, 2014 at 19:11
  • 1 I need to dynamical create a variable off the value of a variable - Why? – jgillich Commented Jun 2, 2014 at 19:11
  • 1 if (window[args[1]+"lastseen"]) ... – mplungjan Commented Jun 2, 2014 at 19:20
Add a ment  | 

1 Answer 1

Reset to default 2

Its just a guess, but it seems like the Variable is just not defined before you eval it.

try this Fiddle.

It works fine for me.

Try to hard-code the variable before you eval it like this:

chkUser = args[1];
var robertlastseen = 'Hardcode';
console.log(eval(chkUser+"lastseen"));

Then you should be able to debug your code.

if you just want to test if this Variable is declared try this:

if(eval('typeof ' + chkUser + 'lastseen') === "undefined"){
    //Variable undefined
}

本文标签: Javascript eval ReferenceError is not definedStack Overflow