有没有办法避免在这段js中进行eval?
// Check requirements Prototype and Scriptaculous
(function () {
var requires = [
'Prototype'
, 'Scriptaculous'
]
while (r = requires.pop()) {
if (eval('typeof ' + r + ' == "undefined"')) alert(r + ' is required');
}
} ());
最佳答案
这里的eval
完全没有意义:
// since everything in the global scope gets defined on 'window'
typeof window[r] === 'undefined';
这将做完全相同相同的事情,还要注意r
泄漏到全局范围内。
// 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');
}
}
} ());
关于javascript - 有没有办法避免在这个 JavaScript block 中进行 eval ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4628518/