几天前,我问了一个关于在外部脚本执行过程中动态修改函数代码的问题,我被告知完全忘记这个概念。我不确定我明白为什么会这样。让我举个例子:
<script>
var display = function(msg)
{
alert(msg);
}
// Now, at the moment, the display() function
// is receiving a single parameter and alerting
// it to the user. I'm now going to use eval()
// to modify the display() function.
eval('display = ' + display.toString().replace('alert(', 'document.write('));
// Now, the display() function writes its parameter
// to the document as opposed to alerting it.
</script>
我意识到这是一个相当微不足道的例子,但肯定有一些用途可以从能够动态修改函数中派生出来,这本身就非常有用。