Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  103] [ 7]  / answers: 1 / hits: 20258  / 14 Years ago, tue, july 20, 2010, 12:00:00

I've been using both in javascript ... really don't know the difference. Googling always shows results for the window object or opening a new window in javascript so couldn't find anything there.



 eval(v+e)
window[v+e]


Sometimes window works for me and at other times eval works ....



So what's the difference between eval() and window[] ?



Sorry for the newbie question though !



Norman


More From » javascript

 Answers
105

Another point that has not been addressed is that eval will resolve the variable reference using the caller variable environment, for example:



var foo = global;

(function () {
var foo = local;
alert(eval(foo)); // alerts local
alert(window[foo]); // alerts global
})();


So as you can see, is not completely equivalent.



If you simply want to reference a global variable, I would recommend you to use the window[prop] approach and avoid surprises.


[#96162] Sunday, July 18, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tristab

Total Points: 735
Total Questions: 106
Total Answers: 96

Location: Grenada
Member since Sun, Dec 20, 2020
3 Years ago
tristab questions
Sat, Sep 25, 21, 00:00, 3 Years ago
Sun, Jan 31, 21, 00:00, 3 Years ago
Wed, Dec 2, 20, 00:00, 4 Years ago
Fri, Oct 23, 20, 00:00, 4 Years ago
;