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 theeval()
.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 thaneval()
and won't throw ReferenceErrors. Or, pass a user object rather than the variable prefix asargs[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
1 Answer
Reset to default 2Its 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
版权声明:本文标题:Javascript eval ReferenceError: is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741817662a2399169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论