Why does an unexecuted eval have an effect on behavior in some browsers?

Viewed 362

Let's suppose I have these two functions:

function change(args) {
    args[0] = "changed";
    return " => ";
}
function f(x) {
    return [x, change(f.arguments), x];
}
console.log(f("original"));

In most browsers, except Opera, this returns ["original", " => ", "original"].

But if I change the f function like this,

function f(x) {
    return [x, change(f.arguments), x];
    eval("");
}

it will return ["original", " => ", "changed"] in IE9, Safari 5, and Firefox 16 and 17.

If I replace eval("") with arguments, it will also change in Chrome.

You can test it on your own browser on jsFiddle.

I don't understand this behavior at all. If the function returns before those statements are executed, how can those statements affect the return value? Even if the statements were executed, why would they have any effect on argument mutation?

3 Answers
Related