Reputation: 5080
Is there a way to avoid eval in this block of js?
// Check requirements Prototype and Scriptaculous
(function () {
var requires = [
'Prototype'
, 'Scriptaculous'
]
while (r = requires.pop()) {
if (eval('typeof ' + r + ' == "undefined"')) alert(r + ' is required');
}
} ());
Upvotes: 0
Views: 442
Reputation: 46745
The eval
here is completely pointless:
// since everything in the global scope gets defined on 'window'
typeof window[r] === 'undefined';
This will do the exact same thing, also note that r
leaks into the global scope.
// Check requirements Prototype and Scriptaculous
(function () {
var requires = ['Prototype', 'Scriptaculous'];
var r = ''; // make sure to __not__ override a global r
while (r = requires.pop()) {
if (typeof window[r] === 'undefined') {
alert(r + ' is required');
}
}
} ());
Upvotes: 3
Reputation: 168988
while (r = requires.pop()) {
if (typeof window[r] == "undefined") alert(r + ' is required');
}
Upvotes: 0