gpt4 book ai didi

javascript - 有没有办法避免在这个 JavaScript block 中进行 eval ?

转载 作者:行者123 更新时间:2023-11-28 16:34:22 25 4
gpt4 key购买 nike

有没有办法避免在这段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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com