有没有一种方法可以避免在此js块中进行评估?
// 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块中,有什么方法可以避免评估?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4628518/