最善の解決策は、コードを文字列として保存するのをやめることです。 代わりに関数を使用してください。
buttonA.script = function() {
do whatever you were doing in your eval
};
// then, instead of `eval(buttonA['script'])`, say...
buttonA.script();
// or, if the name is in a variable...
var foo = 'script'; // for example
buttonA[foo]();
eval
意味があるのは、その性質上、動的に生成または解釈する必要があるコードがある場合だけです。ほとんどの場合、それは正しくありません。実際には、コメントで言及されているテキストエリアスクリプトのテストというケースしか考えられません。
他のすべての場合...
obj = {
first: function() {
function test() { alert('hi'); }
test();
}
};
obj['first']();
// or simply
obj.first();
// and what's more...`test` doesn't escape and trample on stuff.
try { test(); }
catch (ex) { alert("" + ex); } says `test` is not defined