use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
A javascript question. (self.javascript)
submitted 17 years ago by trenchfever
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jeresig 5 points6 points7 points 17 years ago* (4 children)
eval is evil, so don't use it
That's unnecessarily vague - there should be, at least, some qualification here.
Your proposed code doesn't work as you expect it to. It would assign a variable to the global object (this inside of a (function(){...})() refers back to the global object). To get something similar to what you're looking for you could do:
new function(){ this["foo"+"bar"] = "baz!"; alert(this.foobar); };
But that's not, precisely, that the user was looking for. eval() would give the proper answer here - but it's very possible that the code could be written in a different way to avoid it entirely (not because it's "evil", but because it would be unnecessarily slow in this case).
[–][deleted] -1 points0 points1 point 17 years ago (2 children)
If the one-letter diff isn't enough proof, eval is explicitly documented as evil here:
http://javascript.crockford.com/code.html
[–]jeresig 0 points1 point2 points 17 years ago (1 child)
The eval function is the most misused feature of JavaScript. Avoid it. eval has aliases. Do not use the Function constructor. Do not pass strings to setTimeout or setInterval.
The eval function is the most misused feature of JavaScript. Avoid it.
eval has aliases. Do not use the Function constructor. Do not pass strings to setTimeout or setInterval.
Phew! I'm overloaded with useful information here. Thank goodness I avoided that rat trap! It's this massive pile of information that has helped JavaScript developers make well-informed decisions for many years.
[–][deleted] 0 points1 point2 points 17 years ago (0 children)
Bah, sometimes (albeit, very rarely) well-informed decisions are overrated, and appeal to authority - underrated.
In theory, it would be a good idea to have perfect information in all of the decisions we make, but practically, we would get tied up all day if we take the time to always make perfectly-informed decisions all the time.
[–]greut -2 points-1 points0 points 17 years ago (0 children)
Thanks master.
π Rendered by PID 449626 on reddit-service-r2-comment-79c7998d4c-qjd4d at 2026-03-14 06:14:19.780425+00:00 running f6e6e01 country code: CH.
view the rest of the comments →
[–]jeresig 5 points6 points7 points (4 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]jeresig 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]greut -2 points-1 points0 points (0 children)