在这个JavaScript块中有没有避免eval的方法?
在这个js块中有没有避免eval的方法在这个JavaScript块中有没有避免eval的方法?,javascript,eval,Javascript,Eval,在这个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'); } } (
// 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');
}
}
} ());
这里的
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');
}
}
} ());
怎么样
if (typeof window[r] == "undefined")
怎么样
if (typeof window[r] == "undefined")